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