blob: d27313dad42bb4d15ddd95d3d8fb6603e69c32ba [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=(optional<T>&& rhs)
13// noexcept(is_nothrow_move_assignable<T>::value &&
14// is_nothrow_move_constructible<T>::value);
15
Marshall Clow0cdbe602013-11-15 22:42:10 +000016#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000017#include <type_traits>
18#include <cassert>
19
20#if _LIBCPP_STD_VER > 11
21
Marshall Clow0cdbe602013-11-15 22:42:10 +000022using std::experimental::optional;
23
Howard Hinnant01afa5c2013-09-02 20:30:37 +000024struct X
25{
26 static bool throw_now;
27
28 X() = default;
29 X(X&&)
30 {
31 if (throw_now)
32 throw 6;
33 }
34 X& operator=(X&&) noexcept
35 {
36 return *this;
37 }
38};
39
40struct Y {};
41
42bool X::throw_now = false;
43
44#endif // _LIBCPP_STD_VER > 11
45
46int main()
47{
48#if _LIBCPP_STD_VER > 11
49 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000050 static_assert(std::is_nothrow_move_assignable<optional<int>>::value, "");
51 optional<int> opt;
52 constexpr optional<int> opt2;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000053 opt = std::move(opt2);
54 static_assert(static_cast<bool>(opt2) == false, "");
55 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
56 }
57 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000058 optional<int> opt;
59 constexpr optional<int> opt2(2);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000060 opt = std::move(opt2);
61 static_assert(static_cast<bool>(opt2) == true, "");
62 static_assert(*opt2 == 2, "");
63 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
64 assert(*opt == *opt2);
65 }
66 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000067 optional<int> opt(3);
68 constexpr optional<int> opt2;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000069 opt = std::move(opt2);
70 static_assert(static_cast<bool>(opt2) == false, "");
71 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
72 }
73 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000074 optional<int> opt(3);
75 constexpr optional<int> opt2(2);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000076 opt = std::move(opt2);
77 static_assert(static_cast<bool>(opt2) == true, "");
78 static_assert(*opt2 == 2, "");
79 assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
80 assert(*opt == *opt2);
81 }
82 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000083 static_assert(!std::is_nothrow_move_assignable<optional<X>>::value, "");
84 optional<X> opt;
85 optional<X> opt2(X{});
Howard Hinnant01afa5c2013-09-02 20:30:37 +000086 assert(static_cast<bool>(opt2) == true);
87 try
88 {
89 X::throw_now = true;
90 opt = std::move(opt2);
91 assert(false);
92 }
93 catch (int i)
94 {
95 assert(i == 6);
96 assert(static_cast<bool>(opt) == false);
97 }
98 }
99 {
Marshall Clow0cdbe602013-11-15 22:42:10 +0000100 static_assert(std::is_nothrow_move_assignable<optional<Y>>::value, "");
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000101 }
102#endif // _LIBCPP_STD_VER > 11
103}