blob: 8d20be3de19a04541a34057e5608185318ec4a5c [file] [log] [blame]
Howard Hinnant01afa5c2013-09-02 20:30:37 +00001//===----------------------------------------------------------------------===//
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// <optional>
11
12// template <class U, class... Args>
13// void optional<T>::emplace(initializer_list<U> il, Args&&... args);
14
Marshall Clow0cdbe602013-11-15 22:42:10 +000015#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000016#include <type_traits>
17#include <cassert>
18#include <vector>
19
20#if _LIBCPP_STD_VER > 11
21
Marshall Clow0cdbe602013-11-15 22:42:10 +000022using std::experimental::optional;
23
Howard Hinnant01afa5c2013-09-02 20:30:37 +000024class X
25{
26 int i_;
27 int j_ = 0;
28public:
29 static bool dtor_called;
30 constexpr X() : i_(0) {}
31 constexpr X(int i) : i_(i) {}
32 constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
33 ~X() {dtor_called = true;}
34
35 friend constexpr bool operator==(const X& x, const X& y)
36 {return x.i_ == y.i_ && x.j_ == y.j_;}
37};
38
39bool X::dtor_called = false;
40
41class Y
42{
43 int i_;
44 int j_ = 0;
45public:
46 constexpr Y() : i_(0) {}
47 constexpr Y(int i) : i_(i) {}
48 constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
49
50 friend constexpr bool operator==(const Y& x, const Y& y)
51 {return x.i_ == y.i_ && x.j_ == y.j_;}
52};
53
54class Z
55{
56 int i_;
57 int j_ = 0;
58public:
59 static bool dtor_called;
60 constexpr Z() : i_(0) {}
61 constexpr Z(int i) : i_(i) {}
62 constexpr Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
63 {throw 6;}
64 ~Z() {dtor_called = true;}
65
66 friend constexpr bool operator==(const Z& x, const Z& y)
67 {return x.i_ == y.i_ && x.j_ == y.j_;}
68};
69
70bool Z::dtor_called = false;
71
72#endif // _LIBCPP_STD_VER > 11
73
74int main()
75{
76#if _LIBCPP_STD_VER > 11
77 {
78 X x;
79 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000080 optional<X> opt(x);
Howard Hinnant01afa5c2013-09-02 20:30:37 +000081 assert(X::dtor_called == false);
82 opt.emplace({1, 2});
83 assert(X::dtor_called == true);
84 assert(*opt == X({1, 2}));
85 }
86 }
87 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000088 optional<std::vector<int>> opt;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000089 opt.emplace({1, 2, 3}, std::allocator<int>());
90 assert(static_cast<bool>(opt) == true);
91 assert(*opt == std::vector<int>({1, 2, 3}));
92 }
93 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000094 optional<Y> opt;
Howard Hinnant01afa5c2013-09-02 20:30:37 +000095 opt.emplace({1, 2});
96 assert(static_cast<bool>(opt) == true);
97 assert(*opt == Y({1, 2}));
98 }
99 {
100 Z z;
Marshall Clow0cdbe602013-11-15 22:42:10 +0000101 optional<Z> opt(z);
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000102 try
103 {
104 assert(static_cast<bool>(opt) == true);
105 assert(Z::dtor_called == false);
106 opt.emplace({1, 2});
107 }
108 catch (int i)
109 {
110 assert(i == 6);
111 assert(static_cast<bool>(opt) == false);
112 assert(Z::dtor_called == true);
113 }
114 }
115#endif // _LIBCPP_STD_VER > 11
116}