| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 |
// See https://llvm.org/LICENSE.txt for license information. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 |
// |
| 6 |
//===----------------------------------------------------------------------===// |
| 7 |
|
| 8 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 9 |
|
| 10 |
// constexpr const T& value() const &; |
| 11 |
// constexpr T& value() &; |
| 12 |
// constexpr T&& value() &&; |
| 13 |
// constexpr const T&& value() const &&; |
| 14 |
|
| 15 |
#include <cassert> |
| 16 |
#include <concepts> |
| 17 |
#include <expected> |
| 18 |
#include <type_traits> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "test_macros.h" |
| 22 |
|
| 23 |
constexpr bool test() { |
| 24 |
// non-const & |
| 25 |
{ |
| 26 |
std::expected<int, int> e(5); |
| 27 |
decltype(auto) x = e.value(); |
| 28 |
static_assert(std::same_as<decltype(x), int&>); |
| 29 |
assert(&x == &(*e)); |
| 30 |
assert(x == 5); |
| 31 |
} |
| 32 |
|
| 33 |
// const & |
| 34 |
{ |
| 35 |
const std::expected<int, int> e(5); |
| 36 |
decltype(auto) x = e.value(); |
| 37 |
static_assert(std::same_as<decltype(x), const int&>); |
| 38 |
assert(&x == &(*e)); |
| 39 |
assert(x == 5); |
| 40 |
} |
| 41 |
|
| 42 |
// non-const && |
| 43 |
{ |
| 44 |
std::expected<int, int> e(5); |
| 45 |
decltype(auto) x = std::move(e).value(); |
| 46 |
static_assert(std::same_as<decltype(x), int&&>); |
| 47 |
assert(&x == &(*e)); |
| 48 |
assert(x == 5); |
| 49 |
} |
| 50 |
|
| 51 |
// const && |
| 52 |
{ |
| 53 |
const std::expected<int, int> e(5); |
| 54 |
decltype(auto) x = std::move(e).value(); |
| 55 |
static_assert(std::same_as<decltype(x), const int&&>); |
| 56 |
assert(&x == &(*e)); |
| 57 |
assert(x == 5); |
| 58 |
} |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
void testException() { |
| 64 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 65 |
|
| 66 |
// int |
| 67 |
{ |
| 68 |
const std::expected<int, int> e(std::unexpect, 5); |
| 69 |
try { |
| 70 |
(void) e.value(); |
| 71 |
assert(false); |
| 72 |
} catch (const std::bad_expected_access<int>& ex) { |
| 73 |
assert(ex.error() == 5); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#endif // TEST_HAS_NO_EXCEPTIONS |
| 78 |
} |
| 79 |
|
| 80 |
void testAsConst() { |
| 81 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 82 |
struct Error { |
| 83 |
enum { Default, MutableRefCalled, ConstRefCalled } From = Default; |
| 84 |
Error() = default; |
| 85 |
Error(const Error&) { From = ConstRefCalled; } |
| 86 |
Error(Error&) { From = MutableRefCalled; } |
| 87 |
Error(Error&& e) { From = e.From; } |
| 88 |
}; |
| 89 |
|
| 90 |
// Test & overload |
| 91 |
{ |
| 92 |
std::expected<int, Error> e(std::unexpect, Error()); |
| 93 |
try { |
| 94 |
(void)e.value(); |
| 95 |
assert(false); |
| 96 |
} catch (const std::bad_expected_access<Error>& ex) { |
| 97 |
assert(ex.error().From == Error::ConstRefCalled); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// There are no effects for `const &` overload. |
| 102 |
|
| 103 |
#endif // TEST_HAS_NO_EXCEPTIONS |
| 104 |
} |
| 105 |
|
| 106 |
int main(int, char**) { |
| 107 |
test(); |
| 108 |
static_assert(test()); |
| 109 |
testException(); |
| 110 |
testAsConst(); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |