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