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