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// E& error() & noexcept;
12// const E& error() const & noexcept;
13// E&& error() && noexcept;
14// const E&& error() const && noexcept;
15
16#include <cassert>
17#include <concepts>
18#include <expected>
19#include <utility>
20
21template <class T>
22concept ErrorNoexcept =
23 requires(T&& t) {
24 { std::forward<T>(t).error() } noexcept;
25 };
26
27static_assert(!ErrorNoexcept<int>);
28static_assert(ErrorNoexcept<std::bad_expected_access<int>&>);
29static_assert(ErrorNoexcept<std::bad_expected_access<int> const&>);
30static_assert(ErrorNoexcept<std::bad_expected_access<int>&&>);
31static_assert(ErrorNoexcept<std::bad_expected_access<int> const&&>);
32
33void test() {
34 // &
35 {
36 std::bad_expected_access<int> e(5);
37 decltype(auto) i = e.error();
38 static_assert(std::same_as<decltype(i), int&>);
39 assert(i == 5);
40 }
41
42 // const &
43 {
44 const std::bad_expected_access<int> e(5);
45 decltype(auto) i = e.error();
46 static_assert(std::same_as<decltype(i), const int&>);
47 assert(i == 5);
48 }
49
50 // &&
51 {
52 std::bad_expected_access<int> e(5);
53 decltype(auto) i = std::move(e).error();
54 static_assert(std::same_as<decltype(i), int&&>);
55 assert(i == 5);
56 }
57
58 // const &&
59 {
60 const std::bad_expected_access<int> e(5);
61 decltype(auto) i = std::move(e).error();
62 static_assert(std::same_as<decltype(i), const int&&>);
63 assert(i == 5);
64 }
65}
66
67int main(int, char**) {
68 test();
69 return 0;
70}
71

source code of libcxx/test/std/utilities/expected/expected.bad/error.member.pass.cpp