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 |
10 | |
11 | // <optional> |
12 | |
13 | // constexpr const T& optional<T>::value() const &&; |
14 | |
15 | #include <optional> |
16 | #include <type_traits> |
17 | #include <cassert> |
18 | |
19 | #include "test_macros.h" |
20 | |
21 | using std::optional; |
22 | using std::in_place_t; |
23 | using std::in_place; |
24 | using std::bad_optional_access; |
25 | |
26 | struct X |
27 | { |
28 | X() = default; |
29 | X(const X&) = delete; |
30 | constexpr int test() const & {return 3;} |
31 | int test() & {return 4;} |
32 | constexpr int test() const && {return 5;} |
33 | int test() && {return 6;} |
34 | }; |
35 | |
36 | int main(int, char**) |
37 | { |
38 | { |
39 | const optional<X> opt; ((void)opt); |
40 | ASSERT_NOT_NOEXCEPT(std::move(opt).value()); |
41 | ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X const&&); |
42 | } |
43 | { |
44 | constexpr optional<X> opt(in_place); |
45 | static_assert(std::move(opt).value().test() == 5, "" ); |
46 | } |
47 | { |
48 | const optional<X> opt(in_place); |
49 | assert(std::move(opt).value().test() == 5); |
50 | } |
51 | #ifndef TEST_HAS_NO_EXCEPTIONS |
52 | { |
53 | const optional<X> opt; |
54 | try |
55 | { |
56 | (void)std::move(opt).value(); |
57 | assert(false); |
58 | } |
59 | catch (const bad_optional_access&) |
60 | { |
61 | } |
62 | } |
63 | #endif |
64 | |
65 | return 0; |
66 | } |
67 | |