1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11
13 // template <class U, class... Args>
14 // void optional<T>::emplace(initializer_list<U> il, Args&&... args);
16 #include <experimental/optional>
17 #include <type_traits>
21 #include "test_macros.h"
23 using std::experimental::optional;
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;}
36 friend constexpr bool operator==(const X& x, const X& y)
37 {return x.i_ == y.i_ && x.j_ == y.j_;}
40 bool X::dtor_called = false;
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]) {}
51 friend constexpr bool operator==(const Y& x, const Y& y)
52 {return x.i_ == y.i_ && x.j_ == y.j_;}
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])
65 ~Z() {dtor_called = true;}
67 friend constexpr bool operator==(const Z& x, const Z& y)
68 {return x.i_ == y.i_ && x.j_ == y.j_;}
71 bool Z::dtor_called = false;
79 assert(X::dtor_called == false);
81 assert(X::dtor_called == true);
82 assert(*opt == X({1, 2}));
85 X::dtor_called = false;
89 optional<const X> opt(x);
90 assert(X::dtor_called == false);
92 assert(X::dtor_called == true);
93 assert(*opt == X({1, 2}));
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}));
105 assert(static_cast<bool>(opt) == true);
106 assert(*opt == Y({1, 2}));
108 #ifndef TEST_HAS_NO_EXCEPTIONS
114 assert(static_cast<bool>(opt) == true);
115 assert(Z::dtor_called == false);
121 assert(static_cast<bool>(opt) == false);
122 assert(Z::dtor_called == true);