initializer_list.pass.cpp revision 0cdbe6048173c1f05628dbc85430acf191a3e173
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//     constexpr
14//     explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
15
16#include <experimental/optional>
17#include <type_traits>
18#include <vector>
19#include <cassert>
20
21#if _LIBCPP_STD_VER > 11
22
23using std::experimental::optional;
24using std::experimental::in_place_t;
25using std::experimental::in_place;
26
27class X
28{
29    int i_;
30    int j_ = 0;
31public:
32    X() : i_(0) {}
33    X(int i) : i_(i) {}
34    X(int i, int j) : i_(i), j_(j) {}
35
36    ~X() {}
37
38    friend bool operator==(const X& x, const X& y)
39        {return x.i_ == y.i_ && x.j_ == y.j_;}
40};
41
42class Y
43{
44    int i_;
45    int j_ = 0;
46public:
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
55class Z
56{
57    int i_;
58    int j_ = 0;
59public:
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
65    friend constexpr bool operator==(const Z& x, const Z& y)
66        {return x.i_ == y.i_ && x.j_ == y.j_;}
67};
68
69
70#endif  // _LIBCPP_STD_VER > 11
71
72int main()
73{
74#if _LIBCPP_STD_VER > 11
75    {
76        static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, "");
77        static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, "");
78    }
79    {
80        optional<std::vector<int>> opt(in_place, {3, 1});
81        assert(static_cast<bool>(opt) == true);
82        assert((*opt == std::vector<int>{3, 1}));
83        assert(opt->size() == 2);
84    }
85    {
86        optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>());
87        assert(static_cast<bool>(opt) == true);
88        assert((*opt == std::vector<int>{3, 1}));
89        assert(opt->size() == 2);
90    }
91    {
92        static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, "");
93        constexpr optional<Y> opt(in_place, {3, 1});
94        static_assert(static_cast<bool>(opt) == true, "");
95        static_assert(*opt == Y{3, 1}, "");
96
97        struct test_constexpr_ctor
98            : public optional<Y>
99        {
100            constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
101                : optional<Y>(in_place, i) {}
102        };
103
104    }
105    {
106        static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, "");
107        try
108        {
109            optional<Z> opt(in_place, {3, 1});
110            assert(false);
111        }
112        catch (int i)
113        {
114            assert(i == 6);
115        }
116
117        struct test_constexpr_ctor
118            : public optional<Z>
119        {
120            constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
121                : optional<Z>(in_place, i) {}
122        };
123
124    }
125#endif  // _LIBCPP_STD_VER > 11
126}
127