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