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
11 // <variant>
12
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;
19
20 #include "test_macros.h"
21 #include "variant_test_helpers.h"
22 #include <cassert>
23 #include <memory>
24 #include <variant>
25
test_const_get_if()26 void test_const_get_if() {
27 {
28 using V = std::variant<int>;
29 constexpr const V *v = nullptr;
30 static_assert(std::get_if<0>(v) == nullptr, "");
31 }
32 {
33 using V = std::variant<int, const long>;
34 constexpr V v(42);
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, "");
39 }
40 {
41 using V = std::variant<int, const long>;
42 constexpr V v(42l);
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, "");
46 }
47 }
48
test_get_if()49 void test_get_if() {
50 {
51 using V = std::variant<int>;
52 V *v = nullptr;
53 assert(std::get_if<0>(v) == nullptr);
54 }
55 {
56 using V = std::variant<int, long>;
57 V v(42);
58 ASSERT_NOEXCEPT(std::get_if<0>(&v));
59 ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *);
60 assert(*std::get_if<0>(&v) == 42);
61 assert(std::get_if<1>(&v) == nullptr);
62 }
63 {
64 using V = std::variant<int, const long>;
65 V v(42l);
66 ASSERT_SAME_TYPE(decltype(std::get_if<1>(&v)), const long *);
67 assert(*std::get_if<1>(&v) == 42);
68 assert(std::get_if<0>(&v) == nullptr);
69 }
70 }
71
main(int,char **)72 int main(int, char**) {
73 test_const_get_if();
74 test_get_if();
75
76 return 0;
77 }
78