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