special_members.pass.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <optional>
  10. // Make sure we properly generate special member functions for optional<T>
  11. // based on the properties of T itself.
  12. #include <optional>
  13. #include <type_traits>
  14. #include "archetypes.h"
  15. #include "test_macros.h"
  16. template <class T>
  17. struct SpecialMemberTest {
  18. using O = std::optional<T>;
  19. static_assert(std::is_default_constructible_v<O>,
  20. "optional is always default constructible.");
  21. static_assert(std::is_copy_constructible_v<O> == std::is_copy_constructible_v<T>,
  22. "optional<T> is copy constructible if and only if T is copy constructible.");
  23. static_assert(std::is_move_constructible_v<O> ==
  24. (std::is_copy_constructible_v<T> || std::is_move_constructible_v<T>),
  25. "optional<T> is move constructible if and only if T is copy or move constructible.");
  26. static_assert(std::is_copy_assignable_v<O> ==
  27. (std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>),
  28. "optional<T> is copy assignable if and only if T is both copy "
  29. "constructible and copy assignable.");
  30. static_assert(std::is_move_assignable_v<O> ==
  31. ((std::is_move_constructible_v<T> && std::is_move_assignable_v<T>) ||
  32. (std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)),
  33. "optional<T> is move assignable if and only if T is both move constructible and "
  34. "move assignable, or both copy constructible and copy assignable.");
  35. };
  36. template <class ...Args> static void sink(Args&&...) {}
  37. template <class ...TestTypes>
  38. struct DoTestsMetafunction {
  39. DoTestsMetafunction() { sink(SpecialMemberTest<TestTypes>{}...); }
  40. };
  41. int main(int, char**) {
  42. sink(
  43. ImplicitTypes::ApplyTypes<DoTestsMetafunction>{},
  44. ExplicitTypes::ApplyTypes<DoTestsMetafunction>{},
  45. NonLiteralTypes::ApplyTypes<DoTestsMetafunction>{},
  46. NonTrivialTypes::ApplyTypes<DoTestsMetafunction>{}
  47. );
  48. return 0;
  49. }