1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// XFAIL: libcpp-no-exceptions
// <optional>
// template <class U, class... Args>
// void optional<T>::emplace(initializer_list<U> il, Args&&... args);
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#include <vector>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
class X
{
int i_;
int j_ = 0;
public:
static bool dtor_called;
constexpr X() : i_(0) {}
constexpr X(int i) : i_(i) {}
constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
~X() {dtor_called = true;}
friend constexpr bool operator==(const X& x, const X& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
bool X::dtor_called = false;
class Y
{
int i_;
int j_ = 0;
public:
constexpr Y() : i_(0) {}
constexpr Y(int i) : i_(i) {}
constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
friend constexpr bool operator==(const Y& x, const Y& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Z
{
int i_;
int j_ = 0;
public:
static bool dtor_called;
constexpr Z() : i_(0) {}
constexpr Z(int i) : i_(i) {}
constexpr Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
{throw 6;}
~Z() {dtor_called = true;}
friend constexpr bool operator==(const Z& x, const Z& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
bool Z::dtor_called = false;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
X x;
{
optional<X> opt(x);
assert(X::dtor_called == false);
opt.emplace({1, 2});
assert(X::dtor_called == true);
assert(*opt == X({1, 2}));
}
}
{
optional<std::vector<int>> opt;
opt.emplace({1, 2, 3}, std::allocator<int>());
assert(static_cast<bool>(opt) == true);
assert(*opt == std::vector<int>({1, 2, 3}));
}
{
optional<Y> opt;
opt.emplace({1, 2});
assert(static_cast<bool>(opt) == true);
assert(*opt == Y({1, 2}));
}
{
Z z;
optional<Z> opt(z);
try
{
assert(static_cast<bool>(opt) == true);
assert(Z::dtor_called == false);
opt.emplace({1, 2});
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
assert(Z::dtor_called == true);
}
}
#endif // _LIBCPP_STD_VER > 11
}
|