move_only.h 1.0 KB

12345678910111213141516171819202122232425262728
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_COMMON_MOVE_ONLY_H_
  5. #define CARBON_COMMON_MOVE_ONLY_H_
  6. namespace Carbon {
  7. // A base class that indicates a type is move-only. Typically this can be
  8. // achieved by declaring the move constructor and move assignment yourself; this
  9. // type should be used only when doing that is not feasible, such as when
  10. // aggregate initialization is still desired.
  11. //
  12. // This class uses CRTP to ensure that each MoveOnly base class has a different
  13. // type. This is important to avoid the compiler adding extra padding to derived
  14. // classes to give multiple MoveOnly subobjects of the same type different
  15. // addresses.
  16. template <typename Derived>
  17. struct MoveOnly {
  18. MoveOnly() = default;
  19. MoveOnly(MoveOnly&&) noexcept = default;
  20. auto operator=(MoveOnly&&) noexcept -> MoveOnly& = default;
  21. };
  22. } // namespace Carbon
  23. #endif // CARBON_COMMON_MOVE_ONLY_H_