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 // <optional> 11 12 // optional(const optional<T>& rhs); 13 14 #include <experimental/optional> 15 #include <type_traits> 16 #include <cassert> 17 18 #if _LIBCPP_STD_VER > 11 19 20 using std::experimental::optional; 21 22 template <class T> 23 void test(const optional<T> & rhs,bool is_going_to_throw=false)24test(const optional<T>& rhs, bool is_going_to_throw = false) 25 { 26 bool rhs_engaged = static_cast<bool>(rhs); 27 try 28 { 29 optional<T> lhs = rhs; 30 assert(is_going_to_throw == false); 31 assert(static_cast<bool>(lhs) == rhs_engaged); 32 if (rhs_engaged) 33 assert(*lhs == *rhs); 34 } 35 catch (int i) 36 { 37 assert(i == 6); 38 } 39 } 40 41 class X 42 { 43 int i_; 44 public: X(int i)45 X(int i) : i_(i) {} X(const X & x)46 X(const X& x) : i_(x.i_) {} ~X()47 ~X() {i_ = 0;} operator ==(const X & x,const X & y)48 friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} 49 }; 50 51 class Y 52 { 53 int i_; 54 public: Y(int i)55 Y(int i) : i_(i) {} Y(const Y & x)56 Y(const Y& x) : i_(x.i_) {} 57 operator ==(const Y & x,const Y & y)58 friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;} 59 }; 60 61 int count = 0; 62 63 class Z 64 { 65 int i_; 66 public: Z(int i)67 Z(int i) : i_(i) {} Z(const Z &)68 Z(const Z&) 69 { 70 if (++count == 2) 71 throw 6; 72 } 73 operator ==(const Z & x,const Z & y)74 friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;} 75 }; 76 77 78 #endif // _LIBCPP_STD_VER > 11 79 main()80int main() 81 { 82 #if _LIBCPP_STD_VER > 11 83 { 84 typedef int T; 85 optional<T> rhs; 86 test(rhs); 87 } 88 { 89 typedef int T; 90 optional<T> rhs(3); 91 test(rhs); 92 } 93 { 94 typedef X T; 95 optional<T> rhs; 96 test(rhs); 97 } 98 { 99 typedef X T; 100 optional<T> rhs(X(3)); 101 test(rhs); 102 } 103 { 104 typedef Y T; 105 optional<T> rhs; 106 test(rhs); 107 } 108 { 109 typedef Y T; 110 optional<T> rhs(Y(3)); 111 test(rhs); 112 } 113 { 114 typedef Z T; 115 optional<T> rhs; 116 test(rhs); 117 } 118 { 119 typedef Z T; 120 optional<T> rhs(Z(3)); 121 test(rhs, true); 122 } 123 #endif // _LIBCPP_STD_VER > 11 124 } 125