1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // XFAIL: with_system_cxx_lib=macosx10.12
13 // XFAIL: with_system_cxx_lib=macosx10.11
14 // XFAIL: with_system_cxx_lib=macosx10.10
15 // XFAIL: with_system_cxx_lib=macosx10.9
16 // XFAIL: with_system_cxx_lib=macosx10.7
17 // XFAIL: with_system_cxx_lib=macosx10.8
18 
19 // <optional>
20 
21 // constexpr T& optional<T>::value() &;
22 
23 #include <optional>
24 #include <type_traits>
25 #include <cassert>
26 
27 #include "test_macros.h"
28 
29 using std::optional;
30 using std::bad_optional_access;
31 
32 struct X
33 {
34     X() = default;
35     X(const X&) = delete;
36     constexpr int test() const & {return 3;}
37     int test() & {return 4;}
38     constexpr int test() const && {return 5;}
39     int test() && {return 6;}
40 };
41 
42 struct Y
43 {
44     constexpr int test() & {return 7;}
45 };
46 
47 constexpr int
48 test()
49 {
50     optional<Y> opt{Y{}};
51     return opt.value().test();
52 }
53 
54 
55 int main()
56 {
57     {
58         optional<X> opt; ((void)opt);
59         ASSERT_NOT_NOEXCEPT(opt.value());
60         ASSERT_SAME_TYPE(decltype(opt.value()), X&);
61     }
62     {
63         optional<X> opt;
64         opt.emplace();
65         assert(opt.value().test() == 4);
66     }
67 #ifndef TEST_HAS_NO_EXCEPTIONS
68     {
69         optional<X> opt;
70         try
71         {
72             (void)opt.value();
73             assert(false);
74         }
75         catch (const bad_optional_access&)
76         {
77         }
78     }
79 #endif
80     static_assert(test() == 7, "");
81 }
82