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