| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // constexpr void value() const &; |
| 12 | // constexpr void value() &&; |
| 13 | |
| 14 | #include <cassert> |
| 15 | #include <concepts> |
| 16 | #include <expected> |
| 17 | #include <type_traits> |
| 18 | #include <utility> |
| 19 | |
| 20 | #include "MoveOnly.h" |
| 21 | #include "test_macros.h" |
| 22 | |
| 23 | constexpr bool test() { |
| 24 | // const & |
| 25 | { |
| 26 | const std::expected<void, int> e; |
| 27 | e.value(); |
| 28 | static_assert(std::is_same_v<decltype(e.value()), void>); |
| 29 | } |
| 30 | |
| 31 | // & |
| 32 | { |
| 33 | std::expected<void, int> e; |
| 34 | e.value(); |
| 35 | static_assert(std::is_same_v<decltype(e.value()), void>); |
| 36 | } |
| 37 | |
| 38 | // && |
| 39 | { |
| 40 | std::expected<void, int> e; |
| 41 | std::move(e).value(); |
| 42 | static_assert(std::is_same_v<decltype(std::move(e).value()), void>); |
| 43 | } |
| 44 | |
| 45 | // const && |
| 46 | { |
| 47 | const std::expected<void, int> e; |
| 48 | std::move(e).value(); |
| 49 | static_assert(std::is_same_v<decltype(std::move(e).value()), void>); |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | void testException() { |
| 56 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 57 | |
| 58 | // int |
| 59 | { |
| 60 | const std::expected<void, int> e(std::unexpect, 5); |
| 61 | try { |
| 62 | e.value(); |
| 63 | assert(false); |
| 64 | } catch (const std::bad_expected_access<int>& ex) { |
| 65 | assert(ex.error() == 5); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 70 | } |
| 71 | |
| 72 | int main(int, char**) { |
| 73 | test(); |
| 74 | static_assert(test()); |
| 75 | testException(); |
| 76 | return 0; |
| 77 | } |
| 78 | |