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