Debian Codegraph @

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

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