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(const 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 using std::optional;
31
main()32 int main()
33 {
34 {
35 typedef int T;
36 constexpr T t(5);
37 constexpr optional<T> opt(t);
38 static_assert(static_cast<bool>(opt) == true, "");
39 static_assert(*opt == 5, "");
40
41 struct test_constexpr_ctor
42 : public optional<T>
43 {
44 constexpr test_constexpr_ctor(const T&) {}
45 };
46
47 }
48 {
49 typedef double T;
50 constexpr T t(3);
51 constexpr optional<T> opt(t);
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(const T&) {}
59 };
60
61 }
62 {
63 const int x = 42;
64 optional<const int> o(x);
65 assert(*o == x);
66 }
67 {
68 typedef TestTypes::TestType T;
69 T::reset();
70 const T t(3);
71 optional<T> opt = t;
72 assert(T::alive == 2);
73 assert(T::copy_constructed == 1);
74 assert(static_cast<bool>(opt) == true);
75 assert(opt.value().value == 3);
76 }
77 {
78 typedef ExplicitTestTypes::TestType T;
79 static_assert(!std::is_convertible<T const&, optional<T>>::value, "");
80 T::reset();
81 const T t(3);
82 optional<T> opt(t);
83 assert(T::alive == 2);
84 assert(T::copy_constructed == 1);
85 assert(static_cast<bool>(opt) == true);
86 assert(opt.value().value == 3);
87 }
88 {
89 typedef ConstexprTestTypes::TestType T;
90 constexpr T t(3);
91 constexpr optional<T> opt = {t};
92 static_assert(static_cast<bool>(opt) == true, "");
93 static_assert(opt.value().value == 3, "");
94
95 struct test_constexpr_ctor
96 : public optional<T>
97 {
98 constexpr test_constexpr_ctor(const T&) {}
99 };
100 }
101 {
102 typedef ExplicitConstexprTestTypes::TestType T;
103 static_assert(!std::is_convertible<const T&, optional<T>>::value, "");
104 constexpr T t(3);
105 constexpr optional<T> opt(t);
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 #ifndef TEST_HAS_NO_EXCEPTIONS
117 {
118 struct Z {
119 Z(int) {}
120 Z(const Z&) {throw 6;}
121 };
122 typedef Z T;
123 try
124 {
125 const T t(3);
126 optional<T> opt(t);
127 assert(false);
128 }
129 catch (int i)
130 {
131 assert(i == 6);
132 }
133 }
134 #endif
135 }
136