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 13 // template <class U> 14 // optional(optional<U>&& rhs); 15 16 #include <optional> 17 #include <type_traits> 18 #include <memory> 19 #include <cassert> 20 21 #include "test_macros.h" 22 23 using std::optional; 24 25 template <class T, class U> 26 void 27 test(optional<U>&& rhs, bool is_going_to_throw = false) 28 { 29 bool rhs_engaged = static_cast<bool>(rhs); 30 #ifndef TEST_HAS_NO_EXCEPTIONS 31 try 32 { 33 optional<T> lhs = std::move(rhs); 34 assert(is_going_to_throw == false); 35 assert(static_cast<bool>(lhs) == rhs_engaged); 36 } 37 catch (int i) 38 { 39 assert(i == 6); 40 } 41 #else 42 if (is_going_to_throw) return; 43 optional<T> lhs = std::move(rhs); 44 assert(static_cast<bool>(lhs) == rhs_engaged); 45 #endif 46 } 47 48 class X 49 { 50 int i_; 51 public: 52 X(int i) : i_(i) {} 53 X(X&& x) : i_(std::exchange(x.i_, 0)) {} 54 ~X() {i_ = 0;} 55 friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} 56 }; 57 58 int count = 0; 59 60 struct Z 61 { 62 Z(int) { TEST_THROW(6); } 63 }; 64 65 int main() 66 { 67 { 68 optional<short> rhs; 69 test<int>(std::move(rhs)); 70 } 71 { 72 optional<short> rhs(short{3}); 73 test<int>(std::move(rhs)); 74 } 75 { 76 optional<int> rhs; 77 test<X>(std::move(rhs)); 78 } 79 { 80 optional<int> rhs(3); 81 test<X>(std::move(rhs)); 82 } 83 { 84 optional<int> rhs; 85 test<Z>(std::move(rhs)); 86 } 87 { 88 optional<int> rhs(3); 89 test<Z>(std::move(rhs), true); 90 } 91 92 static_assert(!(std::is_constructible<optional<X>, optional<Z>>::value), ""); 93 } 94