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++98, c++03, c++11, c++14 10 // <optional> 11 12 // constexpr T& optional<T>::operator*() &; 13 14 #ifdef _LIBCPP_DEBUG 15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) 16 #endif 17 18 #include <optional> 19 #include <type_traits> 20 #include <cassert> 21 22 #include "test_macros.h" 23 24 using std::optional; 25 26 struct X 27 { 28 constexpr int test() const& {return 3;} 29 int test() & {return 4;} 30 constexpr int test() const&& {return 5;} 31 int test() && {return 6;} 32 }; 33 34 struct Y 35 { 36 constexpr int test() {return 7;} 37 }; 38 39 constexpr int 40 test() 41 { 42 optional<Y> opt{Y{}}; 43 return (*opt).test(); 44 } 45 46 int main(int, char**) 47 { 48 { 49 optional<X> opt; ((void)opt); 50 ASSERT_SAME_TYPE(decltype(*opt), X&); 51 // ASSERT_NOT_NOEXCEPT(*opt); 52 // FIXME: This assertion fails with GCC because it can see that 53 // (A) operator*() is constexpr, and 54 // (B) there is no path through the function that throws. 55 // It's arguable if this is the correct behavior for the noexcept 56 // operator. 57 // Regardless this function should still be noexcept(false) because 58 // it has a narrow contract. 59 } 60 { 61 optional<X> opt(X{}); 62 assert((*opt).test() == 4); 63 } 64 static_assert(test() == 7, ""); 65 #ifdef _LIBCPP_DEBUG 66 { 67 optional<X> opt; 68 assert((*opt).test() == 3); 69 assert(false); 70 } 71 #endif // _LIBCPP_DEBUG 72 73 return 0; 74 } 75