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     int test() & {return 4;}
30     constexpr int test() const&& {return 5;}
31     int test() && {return 6;}
32 };
33 
34 struct Y
35 {
36     int test() const {return 2;}
37 };
38 
39 int main(int, char**)
40 {
41     {
42         const optional<X> opt; ((void)opt);
43         ASSERT_SAME_TYPE(decltype(*opt), X const&);
44         // ASSERT_NOT_NOEXCEPT(*opt);
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         constexpr optional<X> opt(X{});
55         static_assert((*opt).test() == 3, "");
56     }
57     {
58         constexpr optional<Y> opt(Y{});
59         assert((*opt).test() == 2);
60     }
61 #ifdef _LIBCPP_DEBUG
62     {
63         const optional<X> opt;
64         assert((*opt).test() == 3);
65         assert(false);
66     }
67 #endif  // _LIBCPP_DEBUG
68 
69   return 0;
70 }
71