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// <expected>
12
13// template<class F> constexpr auto or_else(F&& f) &;
14// template<class F> constexpr auto or_else(F&& f) const &;
15// template<class F> constexpr auto or_else(F&& f) &&;
16// template<class F> constexpr auto or_else(F&& f) const &&;
17
18#include <expected>
19#include <concepts>
20#include <cassert>
21#include <type_traits>
22#include <utility>
23
24constexpr void test_val_types() {
25 // Test & overload
26 {
27 auto l = [](auto) -> std::expected<void, long> { return {}; };
28 std::expected<void, int> v(std::unexpected<int>(1));
29 std::same_as<std::expected<void, long>> decltype(auto) val = v.or_else(l);
30 assert(val.has_value());
31 }
32
33 // Test const& overload
34 {
35 auto l = [](auto) -> std::expected<void, long> { return {}; };
36 const std::expected<void, int> v(std::unexpected<int>(1));
37 std::same_as<std::expected<void, long>> decltype(auto) val = v.or_else(l);
38 assert(val.has_value());
39 }
40
41 // Test && overload
42 {
43 auto l = [](auto) -> std::expected<void, long> { return {}; };
44 std::expected<void, int> v(std::unexpected<int>(1));
45 std::same_as<std::expected<void, long>> decltype(auto) val = std::move(v).or_else(l);
46 assert(val.has_value());
47 }
48
49 // Test const&& overload
50 {
51 auto l = [](auto) -> std::expected<void, long> { return {}; };
52 const std::expected<void, int> v(std::unexpected<int>(1));
53 std::same_as<std::expected<void, long>> decltype(auto) val = std::move(v).or_else(l);
54 assert(val.has_value());
55 }
56}
57
58constexpr void test_fail() {
59 auto never_called = [](auto) -> std::expected<void, long> {
60 assert(false);
61 return std::expected<void, long>(std::unexpected<long>(5));
62 };
63
64 // Test & overload
65 {
66 std::expected<void, int> v;
67 std::same_as<std::expected<void, long>> decltype(auto) val = v.or_else(never_called);
68 assert(val.has_value());
69 }
70
71 // Test const& overload
72 {
73 const std::expected<void, int> v;
74 std::same_as<std::expected<void, long>> decltype(auto) val = v.or_else(never_called);
75 assert(val.has_value());
76 }
77
78 // Test && overload
79 {
80 std::expected<void, int> v;
81 std::same_as<std::expected<void, long>> decltype(auto) val = std::move(v).or_else(never_called);
82 assert(val.has_value());
83 }
84
85 // Test const&& overload
86 {
87 const std::expected<void, int> v;
88 std::same_as<std::expected<void, long>> decltype(auto) val = std::move(v).or_else(never_called);
89 assert(val.has_value());
90 }
91}
92
93constexpr bool test() {
94 test_fail();
95 test_val_types();
96 return true;
97}
98
99int main(int, char**) {
100 test();
101 static_assert(test());
102
103 return 0;
104}
105

source code of libcxx/test/std/utilities/expected/expected.void/monadic/or_else.pass.cpp