]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/test/type/is_valid.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / test / type / is_valid.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4
5 #include <boost/hana/assert.hpp>
6 #include <boost/hana/equal.hpp>
7 #include <boost/hana/not.hpp>
8 #include <boost/hana/traits.hpp>
9 #include <boost/hana/type.hpp>
10
11 #include <support/tracked.hpp>
12
13 #include <type_traits>
14 namespace hana = boost::hana;
15
16
17 struct undefined { };
18
19 struct static_nested_member { static const int member = 1; };
20 struct static_nested_member_array { static int member[3]; };
21 struct nested_template_struct { template <typename ...> struct nested; };
22 struct nested_template_alias { template <typename ...> using nested = void; };
23
24 int main() {
25 // Check for a non-static member
26 {
27 struct yes { int member; };
28 struct no { };
29
30 auto from_type = hana::is_valid([](auto t) -> decltype(
31 hana::traits::declval(t).member
32 ) { });
33 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
34 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
35
36 auto from_object = hana::is_valid([](auto&& t) -> decltype(
37 t.member
38 ) { });
39 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
40 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
41 }
42
43 // Check for a static member
44 {
45 using yes = static_nested_member;
46 struct no { };
47
48 auto from_type = hana::is_valid([](auto t) -> decltype(
49 decltype(t)::type::member
50 ) { });
51 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
52 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
53
54 auto from_object = hana::is_valid([](auto&& t) -> decltype(
55 std::remove_reference_t<decltype(t)>::member
56 ) { });
57 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
58 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
59 }
60
61 // Check for a nested type
62 {
63 struct yes { using nested = void; };
64 struct no { };
65
66 auto from_type = hana::is_valid([](auto t) -> decltype(hana::type_c<
67 typename decltype(t)::type::nested
68 >) { });
69 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
70 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
71
72 auto from_object = hana::is_valid([](auto&& t) -> decltype(hana::type_c<
73 typename std::remove_reference_t<decltype(t)>::nested
74 >) { });
75 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
76 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
77 }
78
79 // Check for a nested template
80 {
81 { // template struct
82 using yes = nested_template_struct;
83 struct no { };
84
85 auto from_type = hana::is_valid([](auto t) -> decltype(hana::template_<
86 decltype(t)::type::template nested
87 >) { });
88 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
89 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
90
91 auto from_object = hana::is_valid([](auto&& t) -> decltype(hana::template_<
92 std::remove_reference_t<decltype(t)>::template nested
93 >) { });
94 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
95 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
96 }
97
98 { // template alias
99 using yes = nested_template_alias;
100 struct no { };
101
102 auto from_type = hana::is_valid([](auto t) -> decltype(hana::template_<
103 decltype(t)::type::template nested
104 >) { });
105 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
106 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
107
108 auto from_object = hana::is_valid([](auto&& t) -> decltype(hana::template_<
109 std::remove_reference_t<decltype(t)>::template nested
110 >) { });
111 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
112 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
113 }
114 }
115
116 // Make sure that checking for a nested static or non-static member
117 // works even when the type of that member is an array type or
118 // something that can't be returned from a function.
119 {
120 { // non-static member
121 struct yes { int member[3]; };
122 struct no { };
123
124 auto from_type = hana::is_valid([](auto t) -> decltype(
125 (void)hana::traits::declval(t).member
126 ) { });
127 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
128 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
129
130 auto from_object = hana::is_valid([](auto&& t) -> decltype(
131 (void)t.member
132 ) { });
133 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
134 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
135 }
136
137 { // static member
138 using yes = static_nested_member_array;
139 struct no { };
140
141 auto from_type = hana::is_valid([](auto t) -> decltype(
142 (void)decltype(t)::type::member
143 ) { });
144 BOOST_HANA_CONSTANT_CHECK(from_type(hana::type_c<yes>));
145 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_type(hana::type_c<no>)));
146
147 auto from_object = hana::is_valid([](auto&& t) -> decltype(
148 (void)std::remove_reference_t<decltype(t)>::member
149 ) { });
150 BOOST_HANA_CONSTANT_CHECK(from_object(yes{}));
151 BOOST_HANA_CONSTANT_CHECK(hana::not_(from_object(no{})));
152 }
153 }
154
155 // Make sure the result of a `is_valid` function is constexpr
156 // even when called on non-constexpr arguments.
157 {
158 int i;
159 auto f = hana::is_valid([](auto) { });
160 constexpr auto result = f(i);
161 (void)result;
162 }
163
164 // Make sure `is_valid` works with non-PODs.
165 {
166 hana::is_valid(undefined{})(Tracked{1});
167 hana::is_valid([t = Tracked{1}](auto) { return 1; })(Tracked{1});
168 }
169
170 // Check `is_valid` with a nullary function.
171 {
172 auto f = [](auto ...x) { (void)sizeof...(x); /* -Wunused-param */ };
173 auto g = [](auto ...x) -> char(*)[sizeof...(x)] { };
174 BOOST_HANA_CONSTANT_CHECK(hana::is_valid(f)());
175 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::is_valid(g)()));
176 }
177
178 // Call `is_valid` in the non-curried form.
179 {
180 struct yes { int member; };
181 struct no { };
182
183 auto f = [](auto&& t) -> decltype(t.member) { };
184
185 BOOST_HANA_CONSTANT_CHECK(hana::is_valid(f, yes{}));
186 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::is_valid(f, no{})));
187 }
188 }