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;
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 
main(int,char **)36 int main(int, char**)
37 {
38     {
39         const optional<X> opt; ((void)opt);
40         ASSERT_NOT_NOEXCEPT(opt.value());
41         ASSERT_SAME_TYPE(decltype(opt.value()), X const&);
42     }
43     {
44         constexpr optional<X> opt(in_place);
45         static_assert(opt.value().test() == 3, "");
46     }
47     {
48         const optional<X> opt(in_place);
49         assert(opt.value().test() == 3);
50     }
51 #ifndef TEST_HAS_NO_EXCEPTIONS
52     {
53         const optional<X> opt;
54         try
55         {
56             (void)opt.value();
57             assert(false);
58         }
59         catch (const bad_optional_access&)
60         {
61         }
62     }
63 #endif
64 
65   return 0;
66 }
67