]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/optional/optional.object/optional.object.assign/emplace_initializer_list.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / experimental / optional / optional.object / optional.object.assign / emplace_initializer_list.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 // <optional>
12
13 // template <class U, class... Args>
14 //   void optional<T>::emplace(initializer_list<U> il, Args&&... args);
15
16 #include <experimental/optional>
17 #include <type_traits>
18 #include <cassert>
19 #include <vector>
20
21 #include "test_macros.h"
22
23 using std::experimental::optional;
24
25 class X
26 {
27     int i_;
28     int j_ = 0;
29 public:
30     static bool dtor_called;
31     constexpr X() : i_(0) {}
32     constexpr X(int i) : i_(i) {}
33     constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
34     ~X() {dtor_called = true;}
35
36     friend constexpr bool operator==(const X& x, const X& y)
37         {return x.i_ == y.i_ && x.j_ == y.j_;}
38 };
39
40 bool X::dtor_called = false;
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(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
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     int i_;
58     int j_ = 0;
59 public:
60     static bool dtor_called;
61     constexpr Z() : i_(0) {}
62     constexpr Z(int i) : i_(i) {}
63     Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
64         {TEST_THROW(6);}
65     ~Z() {dtor_called = true;}
66
67     friend constexpr bool operator==(const Z& x, const Z& y)
68         {return x.i_ == y.i_ && x.j_ == y.j_;}
69 };
70
71 bool Z::dtor_called = false;
72
73 int main()
74 {
75     {
76         X x;
77         {
78             optional<X> opt(x);
79             assert(X::dtor_called == false);
80             opt.emplace({1, 2});
81             assert(X::dtor_called == true);
82             assert(*opt == X({1, 2}));
83         }
84     }
85     X::dtor_called = false;
86     {
87         X x;
88         {
89             optional<const X> opt(x);
90             assert(X::dtor_called == false);
91             opt.emplace({1, 2});
92             assert(X::dtor_called == true);
93             assert(*opt == X({1, 2}));
94         }
95     }
96     {
97         optional<std::vector<int>> opt;
98         opt.emplace({1, 2, 3}, std::allocator<int>());
99         assert(static_cast<bool>(opt) == true);
100         assert(*opt == std::vector<int>({1, 2, 3}));
101     }
102     {
103         optional<Y> opt;
104         opt.emplace({1, 2});
105         assert(static_cast<bool>(opt) == true);
106         assert(*opt == Y({1, 2}));
107     }
108 #ifndef TEST_HAS_NO_EXCEPTIONS
109     {
110         Z z;
111         optional<Z> opt(z);
112         try
113         {
114             assert(static_cast<bool>(opt) == true);
115             assert(Z::dtor_called == false);
116             opt.emplace({1, 2});
117         }
118         catch (int i)
119         {
120             assert(i == 6);
121             assert(static_cast<bool>(opt) == false);
122             assert(Z::dtor_called == true);
123         }
124     }
125 #endif
126 }