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 // <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 int test() noexcept {return 3;} 29 }; 30 31 struct Y 32 { 33 constexpr int test() {return 3;} 34 }; 35 36 constexpr int 37 test() 38 { 39 optional<Y> opt{Y{}}; 40 return opt->test(); 41 } 42 43 int main(int, char**) 44 { 45 { 46 std::optional<X> opt; ((void)opt); 47 ASSERT_SAME_TYPE(decltype(opt.operator->()), X*); 48 // ASSERT_NOT_NOEXCEPT(opt.operator->()); 49 // FIXME: This assertion fails with GCC because it can see that 50 // (A) operator->() is constexpr, and 51 // (B) there is no path through the function that throws. 52 // It's arguable if this is the correct behavior for the noexcept 53 // operator. 54 // Regardless this function should still be noexcept(false) because 55 // it has a narrow contract. 56 } 57 { 58 optional<X> opt(X{}); 59 assert(opt->test() == 3); 60 } 61 { 62 static_assert(test() == 3, ""); 63 } 64 #ifdef _LIBCPP_DEBUG 65 { 66 optional<X> opt; 67 assert(opt->test() == 3); 68 assert(false); 69 } 70 #endif // _LIBCPP_DEBUG 71 72 return 0; 73 } 74