1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // <variant>
13 
14 //  template <class T, class... Types>
15 //  constexpr add_pointer_t<T> get_if(variant<Types...>* v) noexcept;
16 // template <class T, class... Types>
17 //  constexpr add_pointer_t<const T> get_if(const variant<Types...>* v)
18 //  noexcept;
19 
20 #include "test_macros.h"
21 #include "variant_test_helpers.h"
22 #include <cassert>
23 #include <variant>
24 
test_const_get_if()25 void test_const_get_if() {
26   {
27     using V = std::variant<int>;
28     constexpr const V *v = nullptr;
29     static_assert(std::get_if<int>(v) == nullptr, "");
30   }
31   {
32     using V = std::variant<int, const long>;
33     constexpr V v(42);
34     ASSERT_NOEXCEPT(std::get_if<int>(&v));
35     ASSERT_SAME_TYPE(decltype(std::get_if<int>(&v)), const int *);
36     static_assert(*std::get_if<int>(&v) == 42, "");
37     static_assert(std::get_if<const long>(&v) == nullptr, "");
38   }
39   {
40     using V = std::variant<int, const long>;
41     constexpr V v(42l);
42     ASSERT_SAME_TYPE(decltype(std::get_if<const long>(&v)), const long *);
43     static_assert(*std::get_if<const long>(&v) == 42, "");
44     static_assert(std::get_if<int>(&v) == nullptr, "");
45   }
46 // FIXME: Remove these once reference support is reinstated
47 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
48   {
49     using V = std::variant<int &>;
50     int x = 42;
51     const V v(x);
52     ASSERT_SAME_TYPE(decltype(std::get_if<int &>(&v)), int *);
53     assert(std::get_if<int &>(&v) == &x);
54   }
55   {
56     using V = std::variant<int &&>;
57     int x = 42;
58     const V v(std::move(x));
59     ASSERT_SAME_TYPE(decltype(std::get_if<int &&>(&v)), int *);
60     assert(std::get_if<int &&>(&v) == &x);
61   }
62   {
63     using V = std::variant<const int &&>;
64     int x = 42;
65     const V v(std::move(x));
66     ASSERT_SAME_TYPE(decltype(std::get_if<const int &&>(&v)), const int *);
67     assert(std::get_if<const int &&>(&v) == &x);
68   }
69 #endif
70 }
71 
test_get_if()72 void test_get_if() {
73   {
74     using V = std::variant<int>;
75     V *v = nullptr;
76     assert(std::get_if<int>(v) == nullptr);
77   }
78   {
79     using V = std::variant<int, const long>;
80     V v(42);
81     ASSERT_NOEXCEPT(std::get_if<int>(&v));
82     ASSERT_SAME_TYPE(decltype(std::get_if<int>(&v)), int *);
83     assert(*std::get_if<int>(&v) == 42);
84     assert(std::get_if<const long>(&v) == nullptr);
85   }
86   {
87     using V = std::variant<int, const long>;
88     V v(42l);
89     ASSERT_SAME_TYPE(decltype(std::get_if<const long>(&v)), const long *);
90     assert(*std::get_if<const long>(&v) == 42);
91     assert(std::get_if<int>(&v) == nullptr);
92   }
93 // FIXME: Remove these once reference support is reinstated
94 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
95   {
96     using V = std::variant<int &>;
97     int x = 42;
98     V v(x);
99     ASSERT_SAME_TYPE(decltype(std::get_if<int &>(&v)), int *);
100     assert(std::get_if<int &>(&v) == &x);
101   }
102   {
103     using V = std::variant<const int &>;
104     int x = 42;
105     V v(x);
106     ASSERT_SAME_TYPE(decltype(std::get_if<const int &>(&v)), const int *);
107     assert(std::get_if<const int &>(&v) == &x);
108   }
109   {
110     using V = std::variant<int &&>;
111     int x = 42;
112     V v(std::move(x));
113     ASSERT_SAME_TYPE(decltype(std::get_if<int &&>(&v)), int *);
114     assert(std::get_if<int &&>(&v) == &x);
115   }
116   {
117     using V = std::variant<const int &&>;
118     int x = 42;
119     V v(std::move(x));
120     ASSERT_SAME_TYPE(decltype(std::get_if<const int &&>(&v)), const int *);
121     assert(std::get_if<const int &&>(&v) == &x);
122   }
123 #endif
124 }
125 
main(int,char **)126 int main(int, char**) {
127   test_const_get_if();
128   test_get_if();
129 
130   return 0;
131 }
132