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: with_system_cxx_lib=macosx10.12 && !no-exceptions
14 // XFAIL: with_system_cxx_lib=macosx10.11 && !no-exceptions
15 // XFAIL: with_system_cxx_lib=macosx10.10 && !no-exceptions
16 // XFAIL: with_system_cxx_lib=macosx10.9 && !no-exceptions
17 
18 // constexpr T& optional<T>::value() &&;
19 
20 #include <optional>
21 #include <type_traits>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 
26 using std::optional;
27 using std::bad_optional_access;
28 
29 struct X
30 {
31     X() = default;
32     X(const X&) = delete;
testX33     constexpr int test() const & {return 3;}
testX34     int test() & {return 4;}
testX35     constexpr int test() const && {return 5;}
testX36     int test() && {return 6;}
37 };
38 
39 struct Y
40 {
testY41     constexpr int test() && {return 7;}
42 };
43 
44 constexpr int
test()45 test()
46 {
47     optional<Y> opt{Y{}};
48     return std::move(opt).value().test();
49 }
50 
main(int,char **)51 int main(int, char**)
52 {
53     {
54         optional<X> opt; ((void)opt);
55         ASSERT_NOT_NOEXCEPT(std::move(opt).value());
56         ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&);
57     }
58     {
59         optional<X> opt;
60         opt.emplace();
61         assert(std::move(opt).value().test() == 6);
62     }
63 #ifndef TEST_HAS_NO_EXCEPTIONS
64     {
65         optional<X> opt;
66         try
67         {
68             (void)std::move(opt).value();
69             assert(false);
70         }
71         catch (const bad_optional_access&)
72         {
73         }
74     }
75 #endif
76     static_assert(test() == 7, "");
77 
78   return 0;
79 }
80