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 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // XFAIL: with_system_cxx_lib=macosx10.12
13 // XFAIL: with_system_cxx_lib=macosx10.11
14 // XFAIL: with_system_cxx_lib=macosx10.10
15 // XFAIL: with_system_cxx_lib=macosx10.9
16 // XFAIL: with_system_cxx_lib=macosx10.7
17 // XFAIL: with_system_cxx_lib=macosx10.8
18 
19 // <optional>
20 
21 // constexpr optional(T&& v);
22 
23 #include <optional>
24 #include <type_traits>
25 #include <cassert>
26 
27 #include "test_macros.h"
28 #include "archetypes.hpp"
29 
30 
31 using std::optional;
32 
33 
34 class Z
35 {
36 public:
37     Z(int) {}
38     Z(Z&&) {TEST_THROW(6);}
39 };
40 
41 
42 int main()
43 {
44     {
45         typedef int T;
46         constexpr optional<T> opt(T(5));
47         static_assert(static_cast<bool>(opt) == true, "");
48         static_assert(*opt == 5, "");
49 
50         struct test_constexpr_ctor
51             : public optional<T>
52         {
53             constexpr test_constexpr_ctor(T&&) {}
54         };
55     }
56     {
57         typedef double T;
58         constexpr optional<T> opt(T(3));
59         static_assert(static_cast<bool>(opt) == true, "");
60         static_assert(*opt == 3, "");
61 
62         struct test_constexpr_ctor
63             : public optional<T>
64         {
65             constexpr test_constexpr_ctor(T&&) {}
66         };
67     }
68     {
69         const int x = 42;
70         optional<const int> o(std::move(x));
71         assert(*o == 42);
72     }
73     {
74         typedef TestTypes::TestType T;
75         T::reset();
76         optional<T> opt = T{3};
77         assert(T::alive == 1);
78         assert(T::move_constructed == 1);
79         assert(static_cast<bool>(opt) == true);
80         assert(opt.value().value == 3);
81     }
82     {
83         typedef ExplicitTestTypes::TestType T;
84         static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
85         T::reset();
86         optional<T> opt(T{3});
87         assert(T::alive == 1);
88         assert(T::move_constructed == 1);
89         assert(static_cast<bool>(opt) == true);
90         assert(opt.value().value == 3);
91     }
92     {
93         typedef TestTypes::TestType T;
94         T::reset();
95         optional<T> opt = {3};
96         assert(T::alive == 1);
97         assert(T::value_constructed == 1);
98         assert(T::copy_constructed == 0);
99         assert(T::move_constructed == 0);
100         assert(static_cast<bool>(opt) == true);
101         assert(opt.value().value == 3);
102     }
103     {
104         typedef ConstexprTestTypes::TestType T;
105         constexpr optional<T> opt = {T(3)};
106         static_assert(static_cast<bool>(opt) == true, "");
107         static_assert(opt.value().value == 3, "");
108 
109         struct test_constexpr_ctor
110             : public optional<T>
111         {
112             constexpr test_constexpr_ctor(const T&) {}
113         };
114     }
115     {
116         typedef ConstexprTestTypes::TestType T;
117         constexpr optional<T> opt = {3};
118         static_assert(static_cast<bool>(opt) == true, "");
119         static_assert(opt.value().value == 3, "");
120 
121         struct test_constexpr_ctor
122             : public optional<T>
123         {
124             constexpr test_constexpr_ctor(const T&) {}
125         };
126     }
127     {
128         typedef ExplicitConstexprTestTypes::TestType T;
129         static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
130         constexpr optional<T> opt(T{3});
131         static_assert(static_cast<bool>(opt) == true, "");
132         static_assert(opt.value().value == 3, "");
133 
134         struct test_constexpr_ctor
135             : public optional<T>
136         {
137             constexpr test_constexpr_ctor(T&&) {}
138         };
139 
140     }
141 #ifndef TEST_HAS_NO_EXCEPTIONS
142     {
143         struct Z {
144             Z(int) {}
145             Z(Z&&) {throw 6;}
146         };
147         typedef Z T;
148         try
149         {
150             T t(3);
151             optional<T> opt(std::move(t));
152             assert(false);
153         }
154         catch (int i)
155         {
156             assert(i == 6);
157         }
158     }
159 #endif
160 }
161