TemplateMetaProgrammingCompileTimeTest.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20 * @date 2017
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include <map>
25#include <set>
26#include <string>
27#include <vector>
28
30
31namespace armarx::meta
32{
33 struct Foo
34 {
35 };
36
37 static_assert(std::is_same<int, first_type<int, long, char>>::value, "first_type broken");
38 static_assert(std::is_same<char, last_type<int, long, char>>::value, "last_type broken");
39
40 static_assert(std::is_same<void, nth_type<0, void, long, char>>::value, "nth_type broken");
41 static_assert(std::is_same<long, nth_type<1, void, long, char>>::value, "nth_type broken");
42 static_assert(std::is_same<char, nth_type<2, void, long, char>>::value, "nth_type broken");
43
44 static_assert(std::is_same<void, NthType<0, void, long, char>::type>::value, "NthType broken");
45 static_assert(std::is_same<long, NthType<1, void, long, char>::type>::value, "NthType broken");
46 static_assert(std::is_same<char, NthType<2, void, long, char>::type>::value, "NthType broken");
47
48 static_assert(std::is_same<void, void_t<>>::value, "void_t broken");
49 static_assert(std::is_same<void, void_t<int>>::value, "void_t broken");
50 static_assert(std::is_same<void, void_t<decltype(1)>>::value, "void_t broken");
51
53 "TypeTemplateTraits::IsInstanceOf broken");
55 "TypeTemplateTraits::IsInstanceOf broken");
56
57 static_assert(std::is_same<void, last_type<int, void>>::value, "last_type broken");
58
59 static_assert(HasToString<int>::value, "HasToString broken");
60 static_assert(!HasToString<Foo>::value, "HasToString broken");
61
62 static_assert(HasAtMethod<std::vector<int>, int>::value, "HasAtMethod broken");
63 static_assert(HasAtMethod<std::vector<int>, std::size_t>::value, "HasAtMethod broken");
64 static_assert(HasAtMethod<std::map<std::string, int>, std::string>::value,
65 "HasAtMethod broken");
66 static_assert(!HasAtMethod<std::vector<int>, Foo>::value, "HasAtMethod broken");
67 static_assert(!HasAtMethod<Foo, int>::value, "HasAtMethod broken");
68
69 // names are describing the memberfunction foo
70 // nonstatic / static (n/s)
71 // return void / char (v/c)
72 // accept void / char (v/c)
73 struct nvv
74 {
75 void
76 foo(void)
77 {
78 }
79 };
80
81 struct nvc
82 {
83 void
84 foo(char)
85 {
86 }
87 };
88
89 struct ncv
90 {
91 char
92 foo(void)
93 {
94 return 0;
95 }
96 };
97
98 struct ncc
99 {
100 char
101 foo(char)
102 {
103 return 0;
104 }
105 };
106
107 struct svv
108 {
109 static void
110 foo(void)
111 {
112 }
113 };
114
115 struct svc
116 {
117 static void
118 foo(char)
119 {
120 }
121 };
122
123 struct scv
124 {
125 static char
126 foo(void)
127 {
128 return 0;
129 }
130 };
131
132 struct scc
133 {
134 static char
135 foo(char)
136 {
137 return 0;
138 }
139 };
140
141 ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(hasFoo, foo, void (T::*)(void));
142 ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(hasStaticFoo, foo, void (*)(void));
143
144 static_assert(hasFoo_v<nvv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
145 static_assert(!hasFoo_v<nvc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
146 static_assert(!hasFoo_v<ncv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
147 static_assert(!hasFoo_v<ncc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
148 static_assert(!hasFoo_v<svv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
149 static_assert(!hasFoo_v<svc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
150 static_assert(!hasFoo_v<scv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
151 static_assert(!hasFoo_v<scc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
152
153 static_assert(!hasStaticFoo_v<nvv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
154 static_assert(!hasStaticFoo_v<nvc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
155 static_assert(!hasStaticFoo_v<ncv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
156 static_assert(!hasStaticFoo_v<ncc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
157 static_assert(hasStaticFoo_v<svv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
158 static_assert(!hasStaticFoo_v<svc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
159 static_assert(!hasStaticFoo_v<scv>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
160 static_assert(!hasStaticFoo_v<scc>, "ERROR ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK");
161
162 template <class...>
163 struct Template0;
164 template <class...>
165 struct Template1;
166
168 "TypeTemplateTraits::DisassembleTemplate");
170 "TypeTemplateTraits::DisassembleTemplate");
171 static_assert(std::is_same<TypeTemplateTraits::DisassembleTemplate<
172 Template0<int, void, char>>::ReplaceTemplate<Template1>,
174 "TypeTemplateTraits::DisassembleTemplate::ReplaceTemplate");
175
176 static_assert(std::is_same<TypeTemplateTraits::DisassembleTemplate<
177 Template0<int, void, char>>::ReplaceParameters<void>,
178 Template0<void>>::value,
179 "TypeTemplateTraits::DisassembleTemplate::ReplaceParameters");
180
181
182 static_assert(std::is_same<IndexSequence<0, 1, 2>, MakeIndexSequence<3>>::value,
183 "MakeIndexSequence broken");
184 static_assert(std::is_same<IndexSequence<0, 1, 2>, MakeIndexSequenceFor<int, int, int>>::value,
185 "MakeIndexSequenceFor broken");
186 static_assert(std::is_same<IndexSequence<2, 3, 4>, MakeIndexRange<2, 5>>::value,
187 "MakeIndexRange broken");
188
189 template <class T, class U = int>
190 struct DecayAllTest : std::is_same<typename DecayAll<T>::type, U>
191 {
192 };
193
194 //type
195 static_assert(DecayAllTest<int>::value, "ERROR DecayAll");
196 static_assert(DecayAllTest<int&>::value, "ERROR DecayAll");
197 static_assert(DecayAllTest<int&&>::value, "ERROR DecayAll");
198
199 static_assert(DecayAllTest<const int>::value, "ERROR DecayAll");
200 static_assert(DecayAllTest<const int&>::value, "ERROR DecayAll");
201 static_assert(DecayAllTest<const int&&>::value, "ERROR DecayAll");
202
203 static_assert(DecayAllTest<int*>::value, "ERROR DecayAll");
204 static_assert(DecayAllTest<int* const>::value, "ERROR DecayAll");
205
206 static_assert(DecayAllTest<const int*>::value, "ERROR DecayAll");
207 static_assert(DecayAllTest<const int* const>::value, "ERROR DecayAll");
208
209 //arrays
210 static_assert(DecayAllTest<int[2]>::value, "ERROR DecayAll");
211 static_assert(DecayAllTest<std::add_lvalue_reference<int[2]>::type>::value, "ERROR DecayAll");
212 static_assert(DecayAllTest<std::add_rvalue_reference<int[2]>::type>::value, "ERROR DecayAll");
213
214 static_assert(DecayAllTest<const int[2]>::value, "ERROR DecayAll");
216 "ERROR DecayAll");
218 "ERROR DecayAll");
219
220 static_assert(DecayAllTest<int (*)[2]>::value, "ERROR DecayAll");
221 static_assert(DecayAllTest<int (*const)[2]>::value, "ERROR DecayAll");
222
223 static_assert(DecayAllTest<const int (*)[2]>::value, "ERROR DecayAll");
224 static_assert(DecayAllTest<const int (*const)[2]>::value, "ERROR DecayAll");
225
226 static_assert(DecayAllTest<int(int), int (*)(int)>::value, "ERROR DecayAll");
227} // namespace armarx::meta
typename detail::MakeIndexRange< Lo, Hi >::type MakeIndexRange
typename detail::MakeIndexSequence< N >::type MakeIndexSequence
void void_t
Helper for sfinae (is added in c++17)
MakeIndexSequence< sizeof...(Ts)> MakeIndexSequenceFor
Can be used to determine if T has an at method accepting a type of IdxT.
Can be used to determine if there is an overload for std::to_string for a type T.
Whether a type T is the instance of a given template Template.
#define ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(CHECKNAME, FNCNAME, FNCSIG)