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// template<class G = E> constexpr E error_or(G&& e) const &;
12// template<class G = E> constexpr E error_or(G&& e) &&;
13
14#include <cassert>
15#include <concepts>
16#include <expected>
17#include <type_traits>
18#include <utility>
19
20struct ConstructFromInt {
21 int value;
22 constexpr ConstructFromInt(int v) : value(v) {}
23};
24
25constexpr bool test_default_template_arg() {
26 // const &, has_value()
27 {
28 const std::expected<void, ConstructFromInt> e;
29 std::same_as<ConstructFromInt> decltype(auto) x = e.error_or(10);
30 assert(x.value == 10);
31 }
32
33 // const &, !has_value()
34 {
35 const std::expected<void, ConstructFromInt> e(std::unexpect, 5);
36 std::same_as<ConstructFromInt> decltype(auto) x = e.error_or(10);
37 assert(x.value == 5);
38 }
39
40 // &&, has_value()
41 {
42 const std::expected<void, ConstructFromInt> e;
43 std::same_as<ConstructFromInt> decltype(auto) x = std::move(e).error_or(10);
44 assert(x.value == 10);
45 }
46
47 // &&, !has_value()
48 {
49 const std::expected<void, ConstructFromInt> e(std::unexpect, 5);
50 std::same_as<ConstructFromInt> decltype(auto) x = std::move(e).error_or(10);
51 assert(x.value == 5);
52 }
53
54 return true;
55}
56
57constexpr bool test() {
58 // const &, has_value()
59 {
60 const std::expected<void, int> e;
61 std::same_as<int> decltype(auto) x = e.error_or(10);
62 assert(x == 10);
63 }
64
65 // const &, !has_value()
66 {
67 const std::expected<void, int> e(std::unexpect, 5);
68 std::same_as<int> decltype(auto) x = e.error_or(10);
69 assert(x == 5);
70 }
71
72 // &&, has_value()
73 {
74 std::expected<void, int> e;
75 std::same_as<int> decltype(auto) x = std::move(e).error_or(10);
76 assert(x == 10);
77 }
78
79 // &&, !has_value()
80 {
81 std::expected<void, int> e(std::unexpect, 5);
82 std::same_as<int> decltype(auto) x = std::move(e).error_or(10);
83 assert(x == 5);
84 }
85
86 return true;
87}
88
89int main(int, char**) {
90 test();
91 static_assert(test());
92 test_default_template_arg();
93 static_assert(test_default_template_arg());
94
95 return 0;
96}
97

source code of libcxx/test/std/utilities/expected/expected.void/observers/error_or.pass.cpp