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