blob: 98cb929dc51e3c7c3e412957b24dcdec5b4e0831 [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// constexpr
14// explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
15
Marshall Clow0cdbe602013-11-15 22:42:10 +000016#include <experimental/optional>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000017#include <type_traits>
18#include <vector>
19#include <cassert>
20
21#if _LIBCPP_STD_VER > 11
22
Marshall Clow0cdbe602013-11-15 22:42:10 +000023using std::experimental::optional;
24using std::experimental::in_place_t;
25using std::experimental::in_place;
26
Howard Hinnant01afa5c2013-09-02 20:30:37 +000027class 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, "");
Marshall Clow0cdbe602013-11-15 22:42:10 +000077 static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, "");
Howard Hinnant01afa5c2013-09-02 20:30:37 +000078 }
79 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000080 optional<std::vector<int>> opt(in_place, {3, 1});
Howard Hinnant01afa5c2013-09-02 20:30:37 +000081 assert(static_cast<bool>(opt) == true);
82 assert((*opt == std::vector<int>{3, 1}));
83 assert(opt->size() == 2);
84 }
85 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000086 optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>());
Howard Hinnant01afa5c2013-09-02 20:30:37 +000087 assert(static_cast<bool>(opt) == true);
88 assert((*opt == std::vector<int>{3, 1}));
89 assert(opt->size() == 2);
90 }
91 {
Marshall Clow0cdbe602013-11-15 22:42:10 +000092 static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, "");
93 constexpr optional<Y> opt(in_place, {3, 1});
Howard Hinnant01afa5c2013-09-02 20:30:37 +000094 static_assert(static_cast<bool>(opt) == true, "");
95 static_assert(*opt == Y{3, 1}, "");
96
97 struct test_constexpr_ctor
Marshall Clow0cdbe602013-11-15 22:42:10 +000098 : public optional<Y>
Howard Hinnant01afa5c2013-09-02 20:30:37 +000099 {
Marshall Clow0cdbe602013-11-15 22:42:10 +0000100 constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
101 : optional<Y>(in_place, i) {}
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000102 };
103
104 }
105 {
Marshall Clow0cdbe602013-11-15 22:42:10 +0000106 static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, "");
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000107 try
108 {
Marshall Clow0cdbe602013-11-15 22:42:10 +0000109 optional<Z> opt(in_place, {3, 1});
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000110 assert(false);
111 }
112 catch (int i)
113 {
114 assert(i == 6);
115 }
116
117 struct test_constexpr_ctor
Marshall Clow0cdbe602013-11-15 22:42:10 +0000118 : public optional<Z>
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000119 {
Marshall Clow0cdbe602013-11-15 22:42:10 +0000120 constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
121 : optional<Z>(in_place, i) {}
Howard Hinnant01afa5c2013-09-02 20:30:37 +0000122 };
123
124 }
125#endif // _LIBCPP_STD_VER > 11
126}