1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // Throwing bad_optional_access is supported starting in macosx10.13
12 // XFAIL: with_system_cxx_lib=macosx10.12 && !no-exceptions
13 // XFAIL: with_system_cxx_lib=macosx10.11 && !no-exceptions
14 // XFAIL: with_system_cxx_lib=macosx10.10 && !no-exceptions
15 // XFAIL: with_system_cxx_lib=macosx10.9 && !no-exceptions
16 
17 // <optional>
18 
19 // constexpr optional(const T& v);
20 
21 #include <optional>
22 #include <type_traits>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 #include "archetypes.h"
27 
28 using std::optional;
29 
30 int main(int, char**)
31 {
32     {
33         typedef int T;
34         constexpr T t(5);
35         constexpr optional<T> opt(t);
36         static_assert(static_cast<bool>(opt) == true, "");
37         static_assert(*opt == 5, "");
38 
39         struct test_constexpr_ctor
40             : public optional<T>
41         {
42             constexpr test_constexpr_ctor(const T&) {}
43         };
44 
45     }
46     {
47         typedef double T;
48         constexpr T t(3);
49         constexpr optional<T> opt(t);
50         static_assert(static_cast<bool>(opt) == true, "");
51         static_assert(*opt == 3, "");
52 
53         struct test_constexpr_ctor
54             : public optional<T>
55         {
56             constexpr test_constexpr_ctor(const T&) {}
57         };
58 
59     }
60     {
61         const int x = 42;
62         optional<const int> o(x);
63         assert(*o == x);
64     }
65     {
66         typedef TestTypes::TestType T;
67         T::reset();
68         const T t(3);
69         optional<T> opt = t;
70         assert(T::alive == 2);
71         assert(T::copy_constructed == 1);
72         assert(static_cast<bool>(opt) == true);
73         assert(opt.value().value == 3);
74     }
75     {
76         typedef ExplicitTestTypes::TestType T;
77         static_assert(!std::is_convertible<T const&, optional<T>>::value, "");
78         T::reset();
79         const T t(3);
80         optional<T> opt(t);
81         assert(T::alive == 2);
82         assert(T::copy_constructed == 1);
83         assert(static_cast<bool>(opt) == true);
84         assert(opt.value().value == 3);
85     }
86     {
87         typedef ConstexprTestTypes::TestType T;
88         constexpr T t(3);
89         constexpr optional<T> opt = {t};
90         static_assert(static_cast<bool>(opt) == true, "");
91         static_assert(opt.value().value == 3, "");
92 
93         struct test_constexpr_ctor
94             : public optional<T>
95         {
96             constexpr test_constexpr_ctor(const T&) {}
97         };
98     }
99     {
100         typedef ExplicitConstexprTestTypes::TestType T;
101         static_assert(!std::is_convertible<const T&, optional<T>>::value, "");
102         constexpr T t(3);
103         constexpr optional<T> opt(t);
104         static_assert(static_cast<bool>(opt) == true, "");
105         static_assert(opt.value().value == 3, "");
106 
107         struct test_constexpr_ctor
108             : public optional<T>
109         {
110             constexpr test_constexpr_ctor(const T&) {}
111         };
112 
113     }
114 #ifndef TEST_HAS_NO_EXCEPTIONS
115     {
116         struct Z {
117             Z(int) {}
118             Z(const Z&) {throw 6;}
119         };
120         typedef Z T;
121         try
122         {
123             const T t(3);
124             optional<T> opt(t);
125             assert(false);
126         }
127         catch (int i)
128         {
129             assert(i == 6);
130         }
131     }
132 #endif
133 
134   return 0;
135 }
136