blob: 81c3b4e6dfa27c772eefde0ce6d4282d9a2d477e [file] [log] [blame]
Howard Hinnant01afa5c2013-09-02 20:30:37 +00001//===----------------------------------------------------------------------===//
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
Marshall Clow0cdbe602013-11-15 22:42:10 +000014#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000015#include <type_traits>
16#include <cassert>
17
18#if _LIBCPP_STD_VER > 11
19
Marshall Clow0cdbe602013-11-15 22:42:10 +000020using std::experimental::optional;
21
Howard Hinnant01afa5c2013-09-02 20:30:37 +000022struct 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
34bool X::throw_now = false;
35
36#endif // _LIBCPP_STD_VER > 11
37
38int main()
39{
40#if _LIBCPP_STD_VER > 11
41 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000042 optional<int> opt;
43 constexpr optional<int> opt2;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000044 opt = opt2;
45 static_assert(static_cast<bool>(opt2) == false, "");
46 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
47 }
48 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000049 optional<int> opt;
50 constexpr optional<int> opt2(2);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000051 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 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000058 optional<int> opt(3);
59 constexpr optional<int> opt2;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000060 opt = opt2;
61 static_assert(static_cast<bool>(opt2) == false, "");
62 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
63 }
64 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000065 optional<int> opt(3);
66 constexpr optional<int> opt2(2);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000067 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 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000074 optional<X> opt;
75 optional<X> opt2(X{});
Howard Hinnant01afa5c2013-09-02 20:30:37 +000076 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}