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 | // void optional<T>::emplace(initializer_list<U> il, Args&&... args); |
| 14 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 15 | #include <experimental/optional> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 16 | #include <type_traits> |
| 17 | #include <cassert> |
| 18 | #include <vector> |
| 19 | |
| 20 | #if _LIBCPP_STD_VER > 11 |
| 21 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 22 | using std::experimental::optional; |
| 23 | |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 24 | class X |
| 25 | { |
| 26 | int i_; |
| 27 | int j_ = 0; |
| 28 | public: |
| 29 | static bool dtor_called; |
| 30 | constexpr X() : i_(0) {} |
| 31 | constexpr X(int i) : i_(i) {} |
| 32 | constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {} |
| 33 | ~X() {dtor_called = true;} |
| 34 | |
| 35 | friend constexpr bool operator==(const X& x, const X& y) |
| 36 | {return x.i_ == y.i_ && x.j_ == y.j_;} |
| 37 | }; |
| 38 | |
| 39 | bool X::dtor_called = false; |
| 40 | |
| 41 | class Y |
| 42 | { |
| 43 | int i_; |
| 44 | int j_ = 0; |
| 45 | public: |
| 46 | constexpr Y() : i_(0) {} |
| 47 | constexpr Y(int i) : i_(i) {} |
| 48 | constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {} |
| 49 | |
| 50 | friend constexpr bool operator==(const Y& x, const Y& y) |
| 51 | {return x.i_ == y.i_ && x.j_ == y.j_;} |
| 52 | }; |
| 53 | |
| 54 | class Z |
| 55 | { |
| 56 | int i_; |
| 57 | int j_ = 0; |
| 58 | public: |
| 59 | static bool dtor_called; |
| 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 | ~Z() {dtor_called = true;} |
| 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 | bool Z::dtor_called = false; |
| 71 | |
| 72 | #endif // _LIBCPP_STD_VER > 11 |
| 73 | |
| 74 | int main() |
| 75 | { |
| 76 | #if _LIBCPP_STD_VER > 11 |
| 77 | { |
| 78 | X x; |
| 79 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 80 | optional<X> opt(x); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 81 | assert(X::dtor_called == false); |
| 82 | opt.emplace({1, 2}); |
| 83 | assert(X::dtor_called == true); |
| 84 | assert(*opt == X({1, 2})); |
| 85 | } |
| 86 | } |
| 87 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 88 | optional<std::vector<int>> opt; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 89 | opt.emplace({1, 2, 3}, std::allocator<int>()); |
| 90 | assert(static_cast<bool>(opt) == true); |
| 91 | assert(*opt == std::vector<int>({1, 2, 3})); |
| 92 | } |
| 93 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 94 | optional<Y> opt; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 95 | opt.emplace({1, 2}); |
| 96 | assert(static_cast<bool>(opt) == true); |
| 97 | assert(*opt == Y({1, 2})); |
| 98 | } |
| 99 | { |
| 100 | Z z; |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 101 | optional<Z> opt(z); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 102 | try |
| 103 | { |
| 104 | assert(static_cast<bool>(opt) == true); |
| 105 | assert(Z::dtor_called == false); |
| 106 | opt.emplace({1, 2}); |
| 107 | } |
| 108 | catch (int i) |
| 109 | { |
| 110 | assert(i == 6); |
| 111 | assert(static_cast<bool>(opt) == false); |
| 112 | assert(Z::dtor_called == true); |
| 113 | } |
| 114 | } |
| 115 | #endif // _LIBCPP_STD_VER > 11 |
| 116 | } |