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