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