rvalue_T.pass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. //
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: dylib-has-no-bad_optional_access && !libcpp-no-exceptions
  11. // <optional>
  12. // constexpr optional(T&& v);
  13. #include <optional>
  14. #include <type_traits>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "archetypes.h"
  18. using std::optional;
  19. class Z
  20. {
  21. public:
  22. Z(int) {}
  23. Z(Z&&) {TEST_THROW(6);}
  24. };
  25. int main(int, char**)
  26. {
  27. {
  28. typedef int T;
  29. constexpr optional<T> opt(T(5));
  30. static_assert(static_cast<bool>(opt) == true, "");
  31. static_assert(*opt == 5, "");
  32. struct test_constexpr_ctor
  33. : public optional<T>
  34. {
  35. constexpr test_constexpr_ctor(T&&) {}
  36. };
  37. }
  38. {
  39. typedef double T;
  40. constexpr optional<T> opt(T(3));
  41. static_assert(static_cast<bool>(opt) == true, "");
  42. static_assert(*opt == 3, "");
  43. struct test_constexpr_ctor
  44. : public optional<T>
  45. {
  46. constexpr test_constexpr_ctor(T&&) {}
  47. };
  48. }
  49. {
  50. const int x = 42;
  51. optional<const int> o(std::move(x));
  52. assert(*o == 42);
  53. }
  54. {
  55. typedef TestTypes::TestType T;
  56. T::reset();
  57. optional<T> opt = T{3};
  58. assert(T::alive == 1);
  59. assert(T::move_constructed == 1);
  60. assert(static_cast<bool>(opt) == true);
  61. assert(opt.value().value == 3);
  62. }
  63. {
  64. typedef ExplicitTestTypes::TestType T;
  65. static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
  66. T::reset();
  67. optional<T> opt(T{3});
  68. assert(T::alive == 1);
  69. assert(T::move_constructed == 1);
  70. assert(static_cast<bool>(opt) == true);
  71. assert(opt.value().value == 3);
  72. }
  73. {
  74. typedef TestTypes::TestType T;
  75. T::reset();
  76. optional<T> opt = {3};
  77. assert(T::alive == 1);
  78. assert(T::value_constructed == 1);
  79. assert(T::copy_constructed == 0);
  80. assert(T::move_constructed == 0);
  81. assert(static_cast<bool>(opt) == true);
  82. assert(opt.value().value == 3);
  83. }
  84. {
  85. typedef ConstexprTestTypes::TestType T;
  86. constexpr optional<T> opt = {T(3)};
  87. static_assert(static_cast<bool>(opt) == true, "");
  88. static_assert(opt.value().value == 3, "");
  89. struct test_constexpr_ctor
  90. : public optional<T>
  91. {
  92. constexpr test_constexpr_ctor(const T&) {}
  93. };
  94. }
  95. {
  96. typedef ConstexprTestTypes::TestType T;
  97. constexpr optional<T> opt = {3};
  98. static_assert(static_cast<bool>(opt) == true, "");
  99. static_assert(opt.value().value == 3, "");
  100. struct test_constexpr_ctor
  101. : public optional<T>
  102. {
  103. constexpr test_constexpr_ctor(const T&) {}
  104. };
  105. }
  106. {
  107. typedef ExplicitConstexprTestTypes::TestType T;
  108. static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
  109. constexpr optional<T> opt(T{3});
  110. static_assert(static_cast<bool>(opt) == true, "");
  111. static_assert(opt.value().value == 3, "");
  112. struct test_constexpr_ctor
  113. : public optional<T>
  114. {
  115. constexpr test_constexpr_ctor(T&&) {}
  116. };
  117. }
  118. #ifndef TEST_HAS_NO_EXCEPTIONS
  119. {
  120. struct Z {
  121. Z(int) {}
  122. Z(Z&&) {throw 6;}
  123. };
  124. typedef Z T;
  125. try
  126. {
  127. T t(3);
  128. optional<T> opt(std::move(t));
  129. assert(false);
  130. }
  131. catch (int i)
  132. {
  133. assert(i == 6);
  134. }
  135. }
  136. #endif
  137. return 0;
  138. }