Debian Codegraph @

libcxx/test/std/utilities/expected/expected.expected/observers/deref.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 // constexpr const T& operator*() const & noexcept;
11 // constexpr T& operator*() & noexcept;
12 // constexpr T&& operator*() && noexcept;
13 // constexpr const T&& operator*() const && noexcept;
14
15 #include <cassert>
16 #include <concepts>
17 #include <expected>
18 #include <type_traits>
19 #include <utility>
20
21 #include "test_macros.h"
22
23 // Test noexcept
24 template <class T>
25 concept DerefNoexcept =
26 requires(T t) {
27 { std::forward<T>(t).operator*() } noexcept;
28 };
29
30 static_assert(!DerefNoexcept<int>);
31
32 static_assert(DerefNoexcept<std::expected<int, int>&>);
33 static_assert(DerefNoexcept<const std::expected<int, int>&>);
34 static_assert(DerefNoexcept<std::expected<int, int>&&>);
35 static_assert(DerefNoexcept<const std::expected<int, int>&&>);
36
37 constexpr bool test() {
38 // non-const &
39 {
40 std::expected<int, int> e(5);
41 decltype(auto) x = *e;
42 static_assert(std::same_as<decltype(x), int&>);
43 assert(&x == &(e.value()));
44 assert(x == 5);
45 }
46
47 // const &
48 {
49 const std::expected<int, int> e(5);
50 decltype(auto) x = *e;
51 static_assert(std::same_as<decltype(x), const int&>);
52 assert(&x == &(e.value()));
53 assert(x == 5);
54 }
55
56 // non-const &&
57 {
58 std::expected<int, int> e(5);
59 decltype(auto) x = *std::move(e);
60 static_assert(std::same_as<decltype(x), int&&>);
61 assert(&x == &(e.value()));
62 assert(x == 5);
63 }
64
65 // const &&
66 {
67 const std::expected<int, int> e(5);
68 decltype(auto) x = *std::move(e);
69 static_assert(std::same_as<decltype(x), const int&&>);
70 assert(&x == &(e.value()));
71 assert(x == 5);
72 }
73
74 return true;
75 }
76
77 int main(int, char**) {
78 test();
79 static_assert(test());
80 return 0;
81 }