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#include <optional>
15#include <type_traits>
16#include <cassert>
17
18#include "test_macros.h"
19
20using std::optional;
21
22struct X
23{
24 int test() noexcept {return 3;}
25};
26
27struct Y
28{
29 constexpr int test() {return 3;}
30};
31
32constexpr int
33test()
34{
35 optional<Y> opt{Y{}};
36 return opt->test();
37}
38
39int main(int, char**)
40{
41 {
42 std::optional<X> opt; ((void)opt);
43 ASSERT_SAME_TYPE(decltype(opt.operator->()), X*);
44 // ASSERT_NOT_NOEXCEPT(opt.operator->());
45 // FIXME: This assertion fails with GCC because it can see that
46 // (A) operator->() is constexpr, and
47 // (B) there is no path through the function that throws.
48 // It's arguable if this is the correct behavior for the noexcept
49 // operator.
50 // Regardless this function should still be noexcept(false) because
51 // it has a narrow contract.
52 }
53 {
54 optional<X> opt(X{});
55 assert(opt->test() == 3);
56 }
57 {
58 static_assert(test() == 3, "");
59 }
60
61 return 0;
62}
63

source code of libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp