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 // <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()36int main() 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 opt.value(); 57 assert(false); 58 } 59 catch (const bad_optional_access&) 60 { 61 } 62 } 63 #endif 64 } 65