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