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 // <optional>
11 
12 // Throwing bad_optional_access is supported starting in macosx10.13
13 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
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;
testX30     constexpr int test() const & {return 3;}
testX31     int test() & {return 4;}
testX32     constexpr int test() const && {return 5;}
testX33     int test() && {return 6;}
34 };
35 
36 struct Y
37 {
testY38     constexpr int test() && {return 7;}
39 };
40 
41 constexpr int
test()42 test()
43 {
44     optional<Y> opt{Y{}};
45     return std::move(opt).value().test();
46 }
47 
main(int,char **)48 int main(int, char**)
49 {
50     {
51         optional<X> opt; ((void)opt);
52         ASSERT_NOT_NOEXCEPT(std::move(opt).value());
53         ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&);
54     }
55     {
56         optional<X> opt;
57         opt.emplace();
58         assert(std::move(opt).value().test() == 6);
59     }
60 #ifndef TEST_HAS_NO_EXCEPTIONS
61     {
62         optional<X> opt;
63         try
64         {
65             (void)std::move(opt).value();
66             assert(false);
67         }
68         catch (const bad_optional_access&)
69         {
70         }
71     }
72 #endif
73     static_assert(test() == 7, "");
74 
75   return 0;
76 }
77