| 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 E& error() const & noexcept; |
| 11 |
// constexpr E& error() & noexcept; |
| 12 |
// constexpr E&& error() && noexcept; |
| 13 |
// constexpr const E&& error() const && noexcept; |
| 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 |
// Test noexcept |
| 24 |
template <class T> |
| 25 |
concept ErrorNoexcept = |
| 26 |
requires(T t) { |
| 27 |
{ std::forward<T>(t).error() } noexcept; |
| 28 |
}; |
| 29 |
|
| 30 |
static_assert(!ErrorNoexcept<int>); |
| 31 |
|
| 32 |
static_assert(ErrorNoexcept<std::expected<void, int>&>); |
| 33 |
static_assert(ErrorNoexcept<const std::expected<void, int>&>); |
| 34 |
static_assert(ErrorNoexcept<std::expected<void, int>&&>); |
| 35 |
static_assert(ErrorNoexcept<const std::expected<void, int>&&>); |
| 36 |
|
| 37 |
constexpr bool test() { |
| 38 |
// non-const & |
| 39 |
{ |
| 40 |
std::expected<void, int> e(std::unexpect, 5); |
| 41 |
decltype(auto) x = e.error(); |
| 42 |
static_assert(std::same_as<decltype(x), int&>); |
| 43 |
assert(x == 5); |
| 44 |
} |
| 45 |
|
| 46 |
// const & |
| 47 |
{ |
| 48 |
const std::expected<void, int> e(std::unexpect, 5); |
| 49 |
decltype(auto) x = e.error(); |
| 50 |
static_assert(std::same_as<decltype(x), const int&>); |
| 51 |
assert(x == 5); |
| 52 |
} |
| 53 |
|
| 54 |
// non-const && |
| 55 |
{ |
| 56 |
std::expected<void, int> e(std::unexpect, 5); |
| 57 |
decltype(auto) x = std::move(e).error(); |
| 58 |
static_assert(std::same_as<decltype(x), int&&>); |
| 59 |
assert(x == 5); |
| 60 |
} |
| 61 |
|
| 62 |
// const && |
| 63 |
{ |
| 64 |
const std::expected<void, int> e(std::unexpect, 5); |
| 65 |
decltype(auto) x = std::move(e).error(); |
| 66 |
static_assert(std::same_as<decltype(x), const int&&>); |
| 67 |
assert(x == 5); |
| 68 |
} |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
int main(int, char**) { |
| 74 |
test(); |
| 75 |
static_assert(test()); |
| 76 |
return 0; |
| 77 |
} |