1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
13 // template <size_t I, class... Types>
14 // constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
15 // get_if(variant<Types...>* v) noexcept;
16 // template <size_t I, class... Types>
17 // constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
18 // get_if(const variant<Types...>* v) noexcept;
20 #include "test_macros.h"
21 #include "variant_test_helpers.h"
26 void test_const_get_if() {
28 using V
= std::variant
<int>;
29 constexpr const V
*v
= nullptr;
30 static_assert(std::get_if
<0>(v
) == nullptr, "");
33 using V
= std::variant
<int, const long>;
35 ASSERT_NOEXCEPT(std::get_if
<0>(&v
));
36 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), const int *);
37 static_assert(*std::get_if
<0>(&v
) == 42, "");
38 static_assert(std::get_if
<1>(&v
) == nullptr, "");
41 using V
= std::variant
<int, const long>;
43 ASSERT_SAME_TYPE(decltype(std::get_if
<1>(&v
)), const long *);
44 static_assert(*std::get_if
<1>(&v
) == 42, "");
45 static_assert(std::get_if
<0>(&v
) == nullptr, "");
47 // FIXME: Remove these once reference support is reinstated
48 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
50 using V
= std::variant
<int &>;
53 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), int *);
54 assert(std::get_if
<0>(&v
) == &x
);
57 using V
= std::variant
<int &&>;
59 const V
v(std::move(x
));
60 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), int *);
61 assert(std::get_if
<0>(&v
) == &x
);
64 using V
= std::variant
<const int &&>;
66 const V
v(std::move(x
));
67 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), const int *);
68 assert(std::get_if
<0>(&v
) == &x
);
75 using V
= std::variant
<int>;
77 assert(std::get_if
<0>(v
) == nullptr);
80 using V
= std::variant
<int, long>;
82 ASSERT_NOEXCEPT(std::get_if
<0>(&v
));
83 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), int *);
84 assert(*std::get_if
<0>(&v
) == 42);
85 assert(std::get_if
<1>(&v
) == nullptr);
88 using V
= std::variant
<int, const long>;
90 ASSERT_SAME_TYPE(decltype(std::get_if
<1>(&v
)), const long *);
91 assert(*std::get_if
<1>(&v
) == 42);
92 assert(std::get_if
<0>(&v
) == nullptr);
94 // FIXME: Remove these once reference support is reinstated
95 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
97 using V
= std::variant
<int &>;
100 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), int *);
101 assert(std::get_if
<0>(&v
) == &x
);
104 using V
= std::variant
<const int &>;
107 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), const int *);
108 assert(std::get_if
<0>(&v
) == &x
);
111 using V
= std::variant
<int &&>;
114 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), int *);
115 assert(std::get_if
<0>(&v
) == &x
);
118 using V
= std::variant
<const int &&>;
121 ASSERT_SAME_TYPE(decltype(std::get_if
<0>(&v
)), const int *);
122 assert(std::get_if
<0>(&v
) == &x
);
127 int main(int, char**) {