Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 16 | #include <experimental/optional> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 17 | #include <type_traits> |
| 18 | #include <vector> |
| 19 | #include <cassert> |
| 20 | |
| 21 | #if _LIBCPP_STD_VER > 11 |
| 22 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 23 | using std::experimental::optional; |
| 24 | using std::experimental::in_place_t; |
| 25 | using std::experimental::in_place; |
| 26 | |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 27 | class X |
| 28 | { |
| 29 | int i_; |
| 30 | int j_ = 0; |
| 31 | public: |
| 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 | |
| 42 | class Y |
| 43 | { |
| 44 | int i_; |
| 45 | int j_ = 0; |
| 46 | public: |
| 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 | |
| 55 | class Z |
| 56 | { |
| 57 | int i_; |
| 58 | int j_ = 0; |
| 59 | public: |
| 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 | |
| 72 | int main() |
| 73 | { |
| 74 | #if _LIBCPP_STD_VER > 11 |
| 75 | { |
| 76 | static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, ""); |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 77 | static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, ""); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 78 | } |
| 79 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 80 | optional<std::vector<int>> opt(in_place, {3, 1}); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 81 | assert(static_cast<bool>(opt) == true); |
| 82 | assert((*opt == std::vector<int>{3, 1})); |
| 83 | assert(opt->size() == 2); |
| 84 | } |
| 85 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 86 | optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>()); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 87 | assert(static_cast<bool>(opt) == true); |
| 88 | assert((*opt == std::vector<int>{3, 1})); |
| 89 | assert(opt->size() == 2); |
| 90 | } |
| 91 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 92 | static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, ""); |
| 93 | constexpr optional<Y> opt(in_place, {3, 1}); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 94 | static_assert(static_cast<bool>(opt) == true, ""); |
| 95 | static_assert(*opt == Y{3, 1}, ""); |
| 96 | |
| 97 | struct test_constexpr_ctor |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 98 | : public optional<Y> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 99 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 100 | constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i) |
| 101 | : optional<Y>(in_place, i) {} |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } |
| 105 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 106 | static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, ""); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 107 | try |
| 108 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 109 | optional<Z> opt(in_place, {3, 1}); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 110 | assert(false); |
| 111 | } |
| 112 | catch (int i) |
| 113 | { |
| 114 | assert(i == 6); |
| 115 | } |
| 116 | |
| 117 | struct test_constexpr_ctor |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 118 | : public optional<Z> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 119 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 120 | constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i) |
| 121 | : optional<Z>(in_place, i) {} |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } |
| 125 | #endif // _LIBCPP_STD_VER > 11 |
| 126 | } |