• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: availability=macosx10.13
13 // XFAIL: availability=macosx10.12
14 // XFAIL: availability=macosx10.11
15 // XFAIL: availability=macosx10.10
16 // XFAIL: availability=macosx10.9
17 // XFAIL: availability=macosx10.8
18 // XFAIL: availability=macosx10.7
19 
20 // <optional>
21 
22 // constexpr T& optional<T>::value() &;
23 
24 #include <optional>
25 #include <type_traits>
26 #include <cassert>
27 
28 #include "test_macros.h"
29 
30 using std::optional;
31 using std::bad_optional_access;
32 
33 struct X
34 {
35     X() = default;
36     X(const X&) = delete;
testX37     constexpr int test() const & {return 3;}
testX38     int test() & {return 4;}
testX39     constexpr int test() const && {return 5;}
testX40     int test() && {return 6;}
41 };
42 
43 struct Y
44 {
testY45     constexpr int test() & {return 7;}
46 };
47 
48 constexpr int
test()49 test()
50 {
51     optional<Y> opt{Y{}};
52     return opt.value().test();
53 }
54 
55 
main()56 int main()
57 {
58     {
59         optional<X> opt; ((void)opt);
60         ASSERT_NOT_NOEXCEPT(opt.value());
61         ASSERT_SAME_TYPE(decltype(opt.value()), X&);
62     }
63     {
64         optional<X> opt;
65         opt.emplace();
66         assert(opt.value().test() == 4);
67     }
68 #ifndef TEST_HAS_NO_EXCEPTIONS
69     {
70         optional<X> opt;
71         try
72         {
73             (void)opt.value();
74             assert(false);
75         }
76         catch (const bad_optional_access&)
77         {
78         }
79     }
80 #endif
81     static_assert(test() == 7, "");
82 }
83