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 T& optional<T>::value() &; 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::bad_optional_access; 29 30 struct X 31 { 32 X() = default; 33 X(const X&) = delete; testX34 constexpr int test() const & {return 3;} testX35 int test() & {return 4;} testX36 constexpr int test() const && {return 5;} testX37 int test() && {return 6;} 38 }; 39 40 struct Y 41 { testY42 constexpr int test() & {return 7;} 43 }; 44 45 constexpr int test()46test() 47 { 48 optional<Y> opt{Y{}}; 49 return opt.value().test(); 50 } 51 52 main(int,char **)53int main(int, char**) 54 { 55 { 56 optional<X> opt; ((void)opt); 57 ASSERT_NOT_NOEXCEPT(opt.value()); 58 ASSERT_SAME_TYPE(decltype(opt.value()), X&); 59 } 60 { 61 optional<X> opt; 62 opt.emplace(); 63 assert(opt.value().test() == 4); 64 } 65 #ifndef TEST_HAS_NO_EXCEPTIONS 66 { 67 optional<X> opt; 68 try 69 { 70 (void)opt.value(); 71 assert(false); 72 } 73 catch (const bad_optional_access&) 74 { 75 } 76 } 77 #endif 78 static_assert(test() == 7, ""); 79 80 return 0; 81 } 82