]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/optional/optional.object/optional.object.ctor/in_place_t.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / experimental / optional / optional.object / optional.object.ctor / in_place_t.pass.cpp
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
11
12 // <optional>
13
14 // template <class... Args>
15 //   constexpr explicit optional(in_place_t, Args&&... args);
16
17 #include <experimental/optional>
18 #include <type_traits>
19 #include <cassert>
20
21 #include "test_macros.h"
22
23 using std::experimental::optional;
24 using std::experimental::in_place_t;
25 using std::experimental::in_place;
26
27 class X
28 {
29     int i_;
30     int j_ = 0;
31 public:
32     X() : i_(0) {}
33     X(int i) : i_(i) {}
34     X(int i, int j) : i_(i), j_(j) {}
35
36     ~X() {}
37
38     friend bool operator==(const X& x, const X& y)
39         {return x.i_ == y.i_ && x.j_ == y.j_;}
40 };
41
42 class Y
43 {
44     int i_;
45     int j_ = 0;
46 public:
47     constexpr Y() : i_(0) {}
48     constexpr Y(int i) : i_(i) {}
49     constexpr Y(int i, int j) : i_(i), j_(j) {}
50
51     friend constexpr bool operator==(const Y& x, const Y& y)
52         {return x.i_ == y.i_ && x.j_ == y.j_;}
53 };
54
55 class Z
56 {
57 public:
58     Z(int) {TEST_THROW(6);}
59 };
60
61
62 int main()
63 {
64     {
65         constexpr optional<int> opt(in_place, 5);
66         static_assert(static_cast<bool>(opt) == true, "");
67         static_assert(*opt == 5, "");
68
69         struct test_constexpr_ctor
70             : public optional<int>
71         {
72             constexpr test_constexpr_ctor(in_place_t, int i)
73                 : optional<int>(in_place, i) {}
74         };
75
76     }
77     {
78         const optional<X> opt(in_place);
79         assert(static_cast<bool>(opt) == true);
80         assert(*opt == X());
81     }
82     {
83         const optional<X> opt(in_place, 5);
84         assert(static_cast<bool>(opt) == true);
85         assert(*opt == X(5));
86     }
87     {
88         const optional<X> opt(in_place, 5, 4);
89         assert(static_cast<bool>(opt) == true);
90         assert(*opt == X(5, 4));
91     }
92     {
93         constexpr optional<Y> opt(in_place);
94         static_assert(static_cast<bool>(opt) == true, "");
95         static_assert(*opt == Y(), "");
96
97         struct test_constexpr_ctor
98             : public optional<Y>
99         {
100             constexpr test_constexpr_ctor(in_place_t)
101                 : optional<Y>(in_place) {}
102         };
103
104     }
105     {
106         constexpr optional<Y> opt(in_place, 5);
107         static_assert(static_cast<bool>(opt) == true, "");
108         static_assert(*opt == Y(5), "");
109
110         struct test_constexpr_ctor
111             : public optional<Y>
112         {
113             constexpr test_constexpr_ctor(in_place_t, int i)
114                 : optional<Y>(in_place, i) {}
115         };
116
117     }
118     {
119         constexpr optional<Y> opt(in_place, 5, 4);
120         static_assert(static_cast<bool>(opt) == true, "");
121         static_assert(*opt == Y(5, 4), "");
122
123         struct test_constexpr_ctor
124             : public optional<Y>
125         {
126             constexpr test_constexpr_ctor(in_place_t, int i, int j)
127                 : optional<Y>(in_place, i, j) {}
128         };
129
130     }
131 #ifndef TEST_HAS_NO_EXCEPTIONS
132     {
133         try
134         {
135             const optional<Z> opt(in_place, 1);
136             assert(false);
137         }
138         catch (int i)
139         {
140             assert(i == 6);
141         }
142     }
143 #endif
144 }