• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const 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 using std::optional;
24 
main()25 int main()
26 {
27     {
28         typedef int T;
29         constexpr T t(5);
30         constexpr optional<T> opt(t);
31         static_assert(static_cast<bool>(opt) == true, "");
32         static_assert(*opt == 5, "");
33 
34         struct test_constexpr_ctor
35             : public optional<T>
36         {
37             constexpr test_constexpr_ctor(const T&) {}
38         };
39 
40     }
41     {
42         typedef double T;
43         constexpr T t(3);
44         constexpr optional<T> opt(t);
45         static_assert(static_cast<bool>(opt) == true, "");
46         static_assert(*opt == 3, "");
47 
48         struct test_constexpr_ctor
49             : public optional<T>
50         {
51             constexpr test_constexpr_ctor(const T&) {}
52         };
53 
54     }
55     {
56         const int x = 42;
57         optional<const int> o(x);
58         assert(*o == x);
59     }
60     {
61         typedef TestTypes::TestType T;
62         T::reset();
63         const T t(3);
64         optional<T> opt = t;
65         assert(T::alive == 2);
66         assert(T::copy_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 const&, optional<T>>::value, "");
73         T::reset();
74         const T t(3);
75         optional<T> opt(t);
76         assert(T::alive == 2);
77         assert(T::copy_constructed == 1);
78         assert(static_cast<bool>(opt) == true);
79         assert(opt.value().value == 3);
80     }
81     {
82         typedef ConstexprTestTypes::TestType T;
83         constexpr T t(3);
84         constexpr optional<T> opt = {t};
85         static_assert(static_cast<bool>(opt) == true, "");
86         static_assert(opt.value().value == 3, "");
87 
88         struct test_constexpr_ctor
89             : public optional<T>
90         {
91             constexpr test_constexpr_ctor(const T&) {}
92         };
93     }
94     {
95         typedef ExplicitConstexprTestTypes::TestType T;
96         static_assert(!std::is_convertible<const T&, optional<T>>::value, "");
97         constexpr T t(3);
98         constexpr optional<T> opt(t);
99         static_assert(static_cast<bool>(opt) == true, "");
100         static_assert(opt.value().value == 3, "");
101 
102         struct test_constexpr_ctor
103             : public optional<T>
104         {
105             constexpr test_constexpr_ctor(const T&) {}
106         };
107 
108     }
109 #ifndef TEST_HAS_NO_EXCEPTIONS
110     {
111         struct Z {
112             Z(int) {}
113             Z(const Z&) {throw 6;}
114         };
115         typedef Z T;
116         try
117         {
118             const T t(3);
119             optional<T> opt(t);
120             assert(false);
121         }
122         catch (int i)
123         {
124             assert(i == 6);
125         }
126     }
127 #endif
128 }
129