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 | // optional<T>& operator=(const optional<T>& rhs); |
| 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 | |
| 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 | struct X |
| 23 | { |
| 24 | static bool throw_now; |
| 25 | |
| 26 | X() = default; |
| 27 | X(const X&) |
| 28 | { |
| 29 | if (throw_now) |
| 30 | throw 6; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | bool X::throw_now = false; |
| 35 | |
| 36 | #endif // _LIBCPP_STD_VER > 11 |
| 37 | |
| 38 | int main() |
| 39 | { |
| 40 | #if _LIBCPP_STD_VER > 11 |
| 41 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 42 | optional<int> opt; |
| 43 | constexpr optional<int> opt2; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 44 | opt = opt2; |
| 45 | static_assert(static_cast<bool>(opt2) == false, ""); |
| 46 | assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); |
| 47 | } |
| 48 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 49 | optional<int> opt; |
| 50 | constexpr optional<int> opt2(2); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 51 | opt = opt2; |
| 52 | static_assert(static_cast<bool>(opt2) == true, ""); |
| 53 | static_assert(*opt2 == 2, ""); |
| 54 | assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); |
| 55 | assert(*opt == *opt2); |
| 56 | } |
| 57 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 58 | optional<int> opt(3); |
| 59 | constexpr optional<int> opt2; |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 60 | opt = opt2; |
| 61 | static_assert(static_cast<bool>(opt2) == false, ""); |
| 62 | assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); |
| 63 | } |
| 64 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 65 | optional<int> opt(3); |
| 66 | constexpr optional<int> opt2(2); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 67 | opt = opt2; |
| 68 | static_assert(static_cast<bool>(opt2) == true, ""); |
| 69 | static_assert(*opt2 == 2, ""); |
| 70 | assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); |
| 71 | assert(*opt == *opt2); |
| 72 | } |
| 73 | { |
Marshall Clow | 0cdbe60 | 2013-11-15 22:42:10 +0000 | [diff] [blame] | 74 | optional<X> opt; |
| 75 | optional<X> opt2(X{}); |
Howard Hinnant | 01afa5c | 2013-09-02 20:30:37 +0000 | [diff] [blame] | 76 | assert(static_cast<bool>(opt2) == true); |
| 77 | try |
| 78 | { |
| 79 | X::throw_now = true; |
| 80 | opt = opt2; |
| 81 | assert(false); |
| 82 | } |
| 83 | catch (int i) |
| 84 | { |
| 85 | assert(i == 6); |
| 86 | assert(static_cast<bool>(opt) == false); |
| 87 | } |
| 88 | } |
| 89 | #endif // _LIBCPP_STD_VER > 11 |
| 90 | } |