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++98, c++03, c++11, c++14
10 
11 // XFAIL: dylib-has-no-bad_optional_access && !libcpp-no-exceptions
12 
13 // <optional>
14 
15 // constexpr T& optional<T>::value() &;
16 
17 #include <optional>
18 #include <type_traits>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
23 using std::optional;
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 struct Y
37 {
38     constexpr int test() & {return 7;}
39 };
40 
41 constexpr int
42 test()
43 {
44     optional<Y> opt{Y{}};
45     return opt.value().test();
46 }
47 
48 
49 int main(int, char**)
50 {
51     {
52         optional<X> opt; ((void)opt);
53         ASSERT_NOT_NOEXCEPT(opt.value());
54         ASSERT_SAME_TYPE(decltype(opt.value()), X&);
55     }
56     {
57         optional<X> opt;
58         opt.emplace();
59         assert(opt.value().test() == 4);
60     }
61 #ifndef TEST_HAS_NO_EXCEPTIONS
62     {
63         optional<X> opt;
64         try
65         {
66             (void)opt.value();
67             assert(false);
68         }
69         catch (const bad_optional_access&)
70         {
71         }
72     }
73 #endif
74     static_assert(test() == 7, "");
75 
76   return 0;
77 }
78