• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Throwing bad_optional_access is supported starting in macosx10.13
12 // XFAIL: with_system_cxx_lib=macosx10.12 && !no-exceptions
13 // XFAIL: with_system_cxx_lib=macosx10.11 && !no-exceptions
14 // XFAIL: with_system_cxx_lib=macosx10.10 && !no-exceptions
15 // XFAIL: with_system_cxx_lib=macosx10.9 && !no-exceptions
16 
17 // <optional>
18 
19 // constexpr const T& optional<T>::value() const &;
20 
21 #include <optional>
22 #include <type_traits>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 
27 using std::optional;
28 using std::in_place_t;
29 using std::in_place;
30 using std::bad_optional_access;
31 
32 struct X
33 {
34     X() = default;
35     X(const X&) = delete;
testX36     constexpr int test() const & {return 3;}
testX37     int test() & {return 4;}
testX38     constexpr int test() const && {return 5;}
testX39     int test() && {return 6;}
40 };
41 
main(int,char **)42 int main(int, char**)
43 {
44     {
45         const optional<X> opt; ((void)opt);
46         ASSERT_NOT_NOEXCEPT(opt.value());
47         ASSERT_SAME_TYPE(decltype(opt.value()), X const&);
48     }
49     {
50         constexpr optional<X> opt(in_place);
51         static_assert(opt.value().test() == 3, "");
52     }
53     {
54         const optional<X> opt(in_place);
55         assert(opt.value().test() == 3);
56     }
57 #ifndef TEST_HAS_NO_EXCEPTIONS
58     {
59         const optional<X> opt;
60         try
61         {
62             (void)opt.value();
63             assert(false);
64         }
65         catch (const bad_optional_access&)
66         {
67         }
68     }
69 #endif
70 
71   return 0;
72 }
73