emplace_initializer_list.pass.cpp revision 01afa5c6e407e985d9643707d7b7ab1384bd9317
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// <optional>
11
12// template <class U, class... Args>
13//   void optional<T>::emplace(initializer_list<U> il, Args&&... args);
14
15#include <optional>
16#include <type_traits>
17#include <cassert>
18#include <vector>
19
20#if _LIBCPP_STD_VER > 11
21
22class X
23{
24    int i_;
25    int j_ = 0;
26public:
27    static bool dtor_called;
28    constexpr X() : i_(0) {}
29    constexpr X(int i) : i_(i) {}
30    constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
31    ~X() {dtor_called = true;}
32
33    friend constexpr bool operator==(const X& x, const X& y)
34        {return x.i_ == y.i_ && x.j_ == y.j_;}
35};
36
37bool X::dtor_called = false;
38
39class Y
40{
41    int i_;
42    int j_ = 0;
43public:
44    constexpr Y() : i_(0) {}
45    constexpr Y(int i) : i_(i) {}
46    constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
47
48    friend constexpr bool operator==(const Y& x, const Y& y)
49        {return x.i_ == y.i_ && x.j_ == y.j_;}
50};
51
52class Z
53{
54    int i_;
55    int j_ = 0;
56public:
57    static bool dtor_called;
58    constexpr Z() : i_(0) {}
59    constexpr Z(int i) : i_(i) {}
60    constexpr Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
61        {throw 6;}
62    ~Z() {dtor_called = true;}
63
64    friend constexpr bool operator==(const Z& x, const Z& y)
65        {return x.i_ == y.i_ && x.j_ == y.j_;}
66};
67
68bool Z::dtor_called = false;
69
70#endif  // _LIBCPP_STD_VER > 11
71
72int main()
73{
74#if _LIBCPP_STD_VER > 11
75    {
76        X x;
77        {
78            std::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    {
86        std::optional<std::vector<int>> opt;
87        opt.emplace({1, 2, 3}, std::allocator<int>());
88        assert(static_cast<bool>(opt) == true);
89        assert(*opt == std::vector<int>({1, 2, 3}));
90    }
91    {
92        std::optional<Y> opt;
93        opt.emplace({1, 2});
94        assert(static_cast<bool>(opt) == true);
95        assert(*opt == Y({1, 2}));
96    }
97    {
98        Z z;
99        std::optional<Z> opt(z);
100        try
101        {
102            assert(static_cast<bool>(opt) == true);
103            assert(Z::dtor_called == false);
104            opt.emplace({1, 2});
105        }
106        catch (int i)
107        {
108            assert(i == 6);
109            assert(static_cast<bool>(opt) == false);
110            assert(Z::dtor_called == true);
111        }
112    }
113#endif  // _LIBCPP_STD_VER > 11
114}
115