blob: 999d03d3a8855e6857ad4a6cf69567d10153de2a [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
Asiri Rathnayakecc2e93c2015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Howard Hinnant01afa5c2013-09-02 20:30:37 +000011// <optional>
12
13// optional<T>& operator=(const optional<T>& rhs);
14
Marshall Clow0cdbe602013-11-15 22:42:10 +000015#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000016#include <type_traits>
17#include <cassert>
18
19#if _LIBCPP_STD_VER > 11
20
Marshall Clow0cdbe602013-11-15 22:42:10 +000021using std::experimental::optional;
22
Howard Hinnant01afa5c2013-09-02 20:30:37 +000023struct X
24{
25 static bool throw_now;
26
27 X() = default;
28 X(const X&)
29 {
30 if (throw_now)
31 throw 6;
32 }
33};
34
35bool X::throw_now = false;
36
37#endif // _LIBCPP_STD_VER > 11
38
39int main()
40{
41#if _LIBCPP_STD_VER > 11
42 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000043 optional<int> opt;
44 constexpr optional<int> opt2;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000045 opt = opt2;
46 static_assert(static_cast<bool>(opt2) == false, "");
47 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
48 }
49 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000050 optional<int> opt;
51 constexpr optional<int> opt2(2);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000052 opt = opt2;
53 static_assert(static_cast<bool>(opt2) == true, "");
54 static_assert(*opt2 == 2, "");
55 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
56 assert(*opt == *opt2);
57 }
58 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000059 optional<int> opt(3);
60 constexpr optional<int> opt2;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000061 opt = opt2;
62 static_assert(static_cast<bool>(opt2) == false, "");
63 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
64 }
65 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000066 optional<int> opt(3);
67 constexpr optional<int> opt2(2);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000068 opt = opt2;
69 static_assert(static_cast<bool>(opt2) == true, "");
70 static_assert(*opt2 == 2, "");
71 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
72 assert(*opt == *opt2);
73 }
74 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000075 optional<X> opt;
76 optional<X> opt2(X{});
Howard Hinnant01afa5c2013-09-02 20:30:37 +000077 assert(static_cast<bool>(opt2) == true);
78 try
79 {
80 X::throw_now = true;
81 opt = opt2;
82 assert(false);
83 }
84 catch (int i)
85 {
86 assert(i == 6);
87 assert(static_cast<bool>(opt) == false);
88 }
89 }
90#endif // _LIBCPP_STD_VER > 11
91}