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