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// template<class U> constexpr T value_or(U&& v) const &;
11// template<class U> constexpr T value_or(U&& v) &&;
12
13#include <cassert>
14#include <concepts>
15#include <expected>
16#include <type_traits>
17#include <utility>
18
19#include "MoveOnly.h"
20#include "test_macros.h"
21
22constexpr bool test() {
23 // const &, has_value()
24 {
25 const std::expected<int, int> e(5);
26 std::same_as<int> decltype(auto) x = e.value_or(10);
27 assert(x == 5);
28 }
29
30 // const &, !has_value()
31 {
32 const std::expected<int, int> e(std::unexpect, 5);
33 std::same_as<int> decltype(auto) x = e.value_or(10);
34 assert(x == 10);
35 }
36
37 // &&, has_value()
38 {
39 std::expected<MoveOnly, int> e(std::in_place, 5);
40 std::same_as<MoveOnly> decltype(auto) x = std::move(e).value_or(10);
41 assert(x == 5);
42 }
43
44 // &&, !has_value()
45 {
46 std::expected<MoveOnly, int> e(std::unexpect, 5);
47 std::same_as<MoveOnly> decltype(auto) x = std::move(e).value_or(10);
48 assert(x == 10);
49 }
50
51 return true;
52}
53
54int main(int, char**) {
55 test();
56 static_assert(test());
57 return 0;
58}
59

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