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<T>& operator=(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 struct X
23 {
24     static bool throw_now;
25 
26     X() = default;
27     X(const X&)
28     {
29         if (throw_now)
30             throw 6;
31     }
32 };
33 
34 bool X::throw_now = false;
35 
36 #endif  // _LIBCPP_STD_VER > 11
37 
38 int main()
39 {
40 #if _LIBCPP_STD_VER > 11
41     {
42         optional<int> opt;
43         constexpr optional<int> opt2;
44         opt = opt2;
45         static_assert(static_cast<bool>(opt2) == false, "");
46         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
47     }
48     {
49         optional<int> opt;
50         constexpr optional<int> opt2(2);
51         opt = opt2;
52         static_assert(static_cast<bool>(opt2) == true, "");
53         static_assert(*opt2 == 2, "");
54         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
55         assert(*opt == *opt2);
56     }
57     {
58         optional<int> opt(3);
59         constexpr optional<int> opt2;
60         opt = opt2;
61         static_assert(static_cast<bool>(opt2) == false, "");
62         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
63     }
64     {
65         optional<int> opt(3);
66         constexpr optional<int> opt2(2);
67         opt = opt2;
68         static_assert(static_cast<bool>(opt2) == true, "");
69         static_assert(*opt2 == 2, "");
70         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
71         assert(*opt == *opt2);
72     }
73     {
74         optional<X> opt;
75         optional<X> opt2(X{});
76         assert(static_cast<bool>(opt2) == true);
77         try
78         {
79             X::throw_now = true;
80             opt = opt2;
81             assert(false);
82         }
83         catch (int i)
84         {
85             assert(i == 6);
86             assert(static_cast<bool>(opt) == false);
87         }
88     }
89 #endif  // _LIBCPP_STD_VER > 11
90 }
91