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