rvalue_T.pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14
  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.hpp"
  18. using std::optional;
  19. class Z
  20. {
  21. public:
  22. Z(int) {}
  23. Z(Z&&) {TEST_THROW(6);}
  24. };
  25. int main()
  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. typedef TestTypes::TestType T;
  51. T::reset();
  52. optional<T> opt = T{3};
  53. assert(T::alive == 1);
  54. assert(T::move_constructed == 1);
  55. assert(static_cast<bool>(opt) == true);
  56. assert(opt.value().value == 3);
  57. }
  58. {
  59. typedef ExplicitTestTypes::TestType T;
  60. static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
  61. T::reset();
  62. optional<T> opt(T{3});
  63. assert(T::alive == 1);
  64. assert(T::move_constructed == 1);
  65. assert(static_cast<bool>(opt) == true);
  66. assert(opt.value().value == 3);
  67. }
  68. {
  69. typedef TestTypes::TestType T;
  70. T::reset();
  71. optional<T> opt = {3};
  72. assert(T::alive == 1);
  73. assert(T::value_constructed == 1);
  74. assert(T::copy_constructed == 0);
  75. assert(T::move_constructed == 0);
  76. assert(static_cast<bool>(opt) == true);
  77. assert(opt.value().value == 3);
  78. }
  79. {
  80. typedef ConstexprTestTypes::TestType T;
  81. constexpr optional<T> opt = {T(3)};
  82. static_assert(static_cast<bool>(opt) == true, "");
  83. static_assert(opt.value().value == 3, "");
  84. struct test_constexpr_ctor
  85. : public optional<T>
  86. {
  87. constexpr test_constexpr_ctor(const T&) {}
  88. };
  89. }
  90. {
  91. typedef ConstexprTestTypes::TestType T;
  92. constexpr optional<T> opt = {3};
  93. static_assert(static_cast<bool>(opt) == true, "");
  94. static_assert(opt.value().value == 3, "");
  95. struct test_constexpr_ctor
  96. : public optional<T>
  97. {
  98. constexpr test_constexpr_ctor(const T&) {}
  99. };
  100. }
  101. {
  102. typedef ExplicitConstexprTestTypes::TestType T;
  103. static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
  104. constexpr optional<T> opt(T{3});
  105. static_assert(static_cast<bool>(opt) == true, "");
  106. static_assert(opt.value().value == 3, "");
  107. struct test_constexpr_ctor
  108. : public optional<T>
  109. {
  110. constexpr test_constexpr_ctor(T&&) {}
  111. };
  112. }
  113. #ifndef TEST_HAS_NO_EXCEPTIONS
  114. {
  115. struct Z {
  116. Z(int) {}
  117. Z(Z&&) {throw 6;}
  118. };
  119. typedef Z T;
  120. try
  121. {
  122. T t(3);
  123. optional<T> opt(std::move(t));
  124. assert(false);
  125. }
  126. catch (int i)
  127. {
  128. assert(i == 6);
  129. }
  130. }
  131. #endif
  132. }