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 | // constexpr optional(T&& v); |
| 13 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 14 | #include <experimental/optional> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 15 | #include <type_traits> |
| 16 | #include <cassert> |
| 17 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 18 | #if _LIBCPP_STD_VER > 11 |
| 19 | |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 20 | using std::experimental::optional; |
| 21 | |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 22 | class X |
| 23 | { |
| 24 | int i_; |
| 25 | public: |
| 26 | X(int i) : i_(i) {} |
| 27 | X(X&& x) : i_(x.i_) {} |
| 28 | |
| 29 | friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} |
| 30 | }; |
| 31 | |
| 32 | class Y |
| 33 | { |
| 34 | int i_; |
| 35 | public: |
| 36 | constexpr Y(int i) : i_(i) {} |
| 37 | constexpr Y(Y&& x) : i_(x.i_) {} |
| 38 | |
| 39 | friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;} |
| 40 | }; |
| 41 | |
| 42 | class Z |
| 43 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 44 | int i_; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 45 | public: |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 46 | Z(int i) : i_(i) {} |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 47 | Z(Z&&) {throw 6;} |
| 48 | }; |
| 49 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 50 | #endif // _LIBCPP_STD_VER > 11 |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 51 | |
| 52 | int main() |
| 53 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 54 | #if _LIBCPP_STD_VER > 11 |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 55 | { |
| 56 | typedef int T; |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 57 | constexpr optional<T> opt(T(5)); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 58 | static_assert(static_cast<bool>(opt) == true, ""); |
| 59 | static_assert(*opt == 5, ""); |
| 60 | |
| 61 | struct test_constexpr_ctor |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 62 | : public optional<T> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 63 | { |
| 64 | constexpr test_constexpr_ctor(T&&) {} |
| 65 | }; |
| 66 | } |
| 67 | { |
| 68 | typedef double T; |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 69 | constexpr optional<T> opt(T(3)); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 70 | static_assert(static_cast<bool>(opt) == true, ""); |
| 71 | static_assert(*opt == 3, ""); |
| 72 | |
| 73 | struct test_constexpr_ctor |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 74 | : public optional<T> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 75 | { |
| 76 | constexpr test_constexpr_ctor(T&&) {} |
| 77 | }; |
| 78 | } |
| 79 | { |
| 80 | typedef X T; |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 81 | optional<T> opt(T(3)); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 82 | assert(static_cast<bool>(opt) == true); |
| 83 | assert(*opt == 3); |
| 84 | } |
| 85 | { |
| 86 | typedef Y T; |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 87 | constexpr optional<T> opt(T(3)); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 88 | static_assert(static_cast<bool>(opt) == true, ""); |
| 89 | static_assert(*opt == 3, ""); |
| 90 | |
| 91 | struct test_constexpr_ctor |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 92 | : public optional<T> |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 93 | { |
| 94 | constexpr test_constexpr_ctor(T&&) {} |
| 95 | }; |
| 96 | } |
| 97 | { |
| 98 | typedef Z T; |
| 99 | try |
| 100 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 101 | optional<T> opt(T(3)); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 102 | assert(false); |
| 103 | } |
| 104 | catch (int i) |
| 105 | { |
| 106 | assert(i == 6); |
| 107 | } |
| 108 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 109 | #endif // _LIBCPP_STD_VER > 11 |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 110 | } |