1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/meta/type_traits.h"
16
17 #include <cstdint>
18 #include <string>
19 #include <type_traits>
20 #include <utility>
21 #include <vector>
22
23 #include "gtest/gtest.h"
24
25 namespace {
26
27 using ::testing::StaticAssertTypeEq;
28
29 template <class T, class U>
30 struct simple_pair {
31 T first;
32 U second;
33 };
34
35 struct Dummy {};
36
37 struct ReturnType {};
38 struct ConvertibleToReturnType {
39 operator ReturnType() const; // NOLINT
40 };
41
42 // Unique types used as parameter types for testing the detection idiom.
43 struct StructA {};
44 struct StructB {};
45 struct StructC {};
46
47 struct TypeWithBarFunction {
48 template <class T,
49 absl::enable_if_t<std::is_same<T&&, StructA&>::value, int> = 0>
50 ReturnType bar(T&&, const StructB&, StructC&&) &&; // NOLINT
51 };
52
53 struct TypeWithBarFunctionAndConvertibleReturnType {
54 template <class T,
55 absl::enable_if_t<std::is_same<T&&, StructA&>::value, int> = 0>
56 ConvertibleToReturnType bar(T&&, const StructB&, StructC&&) &&; // NOLINT
57 };
58
59 template <class Class, class... Ts>
60 using BarIsCallableImpl =
61 decltype(std::declval<Class>().bar(std::declval<Ts>()...));
62
63 template <class Class, class... T>
64 using BarIsCallable =
65 absl::type_traits_internal::is_detected<BarIsCallableImpl, Class, T...>;
66
67 template <class Class, class... T>
68 using BarIsCallableConv = absl::type_traits_internal::is_detected_convertible<
69 ReturnType, BarIsCallableImpl, Class, T...>;
70
71 // NOTE: Test of detail type_traits_internal::is_detected.
TEST(IsDetectedTest,BasicUsage)72 TEST(IsDetectedTest, BasicUsage) {
73 EXPECT_TRUE((BarIsCallable<TypeWithBarFunction, StructA&, const StructB&,
74 StructC>::value));
75 EXPECT_TRUE(
76 (BarIsCallable<TypeWithBarFunction, StructA&, StructB&, StructC>::value));
77 EXPECT_TRUE(
78 (BarIsCallable<TypeWithBarFunction, StructA&, StructB, StructC>::value));
79
80 EXPECT_FALSE((BarIsCallable<int, StructA&, const StructB&, StructC>::value));
81 EXPECT_FALSE((BarIsCallable<TypeWithBarFunction&, StructA&, const StructB&,
82 StructC>::value));
83 EXPECT_FALSE((BarIsCallable<TypeWithBarFunction, StructA, const StructB&,
84 StructC>::value));
85 }
86
87 // NOTE: Test of detail type_traits_internal::is_detected_convertible.
TEST(IsDetectedConvertibleTest,BasicUsage)88 TEST(IsDetectedConvertibleTest, BasicUsage) {
89 EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunction, StructA&, const StructB&,
90 StructC>::value));
91 EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunction, StructA&, StructB&,
92 StructC>::value));
93 EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunction, StructA&, StructB,
94 StructC>::value));
95 EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType,
96 StructA&, const StructB&, StructC>::value));
97 EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType,
98 StructA&, StructB&, StructC>::value));
99 EXPECT_TRUE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType,
100 StructA&, StructB, StructC>::value));
101
102 EXPECT_FALSE(
103 (BarIsCallableConv<int, StructA&, const StructB&, StructC>::value));
104 EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunction&, StructA&,
105 const StructB&, StructC>::value));
106 EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunction, StructA, const StructB&,
107 StructC>::value));
108 EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType&,
109 StructA&, const StructB&, StructC>::value));
110 EXPECT_FALSE((BarIsCallableConv<TypeWithBarFunctionAndConvertibleReturnType,
111 StructA, const StructB&, StructC>::value));
112 }
113
TEST(VoidTTest,BasicUsage)114 TEST(VoidTTest, BasicUsage) {
115 StaticAssertTypeEq<void, absl::void_t<Dummy>>();
116 StaticAssertTypeEq<void, absl::void_t<Dummy, Dummy, Dummy>>();
117 }
118
TEST(ConjunctionTest,BasicBooleanLogic)119 TEST(ConjunctionTest, BasicBooleanLogic) {
120 EXPECT_TRUE(absl::conjunction<>::value);
121 EXPECT_TRUE(absl::conjunction<std::true_type>::value);
122 EXPECT_TRUE((absl::conjunction<std::true_type, std::true_type>::value));
123 EXPECT_FALSE((absl::conjunction<std::true_type, std::false_type>::value));
124 EXPECT_FALSE((absl::conjunction<std::false_type, std::true_type>::value));
125 EXPECT_FALSE((absl::conjunction<std::false_type, std::false_type>::value));
126 }
127
128 struct MyTrueType {
129 static constexpr bool value = true;
130 };
131
132 struct MyFalseType {
133 static constexpr bool value = false;
134 };
135
TEST(ConjunctionTest,ShortCircuiting)136 TEST(ConjunctionTest, ShortCircuiting) {
137 EXPECT_FALSE(
138 (absl::conjunction<std::true_type, std::false_type, Dummy>::value));
139 EXPECT_TRUE((std::is_base_of<MyFalseType,
140 absl::conjunction<std::true_type, MyFalseType,
141 std::false_type>>::value));
142 EXPECT_TRUE(
143 (std::is_base_of<MyTrueType,
144 absl::conjunction<std::true_type, MyTrueType>>::value));
145 }
146
TEST(DisjunctionTest,BasicBooleanLogic)147 TEST(DisjunctionTest, BasicBooleanLogic) {
148 EXPECT_FALSE(absl::disjunction<>::value);
149 EXPECT_FALSE(absl::disjunction<std::false_type>::value);
150 EXPECT_TRUE((absl::disjunction<std::true_type, std::true_type>::value));
151 EXPECT_TRUE((absl::disjunction<std::true_type, std::false_type>::value));
152 EXPECT_TRUE((absl::disjunction<std::false_type, std::true_type>::value));
153 EXPECT_FALSE((absl::disjunction<std::false_type, std::false_type>::value));
154 }
155
TEST(DisjunctionTest,ShortCircuiting)156 TEST(DisjunctionTest, ShortCircuiting) {
157 EXPECT_TRUE(
158 (absl::disjunction<std::false_type, std::true_type, Dummy>::value));
159 EXPECT_TRUE((
160 std::is_base_of<MyTrueType, absl::disjunction<std::false_type, MyTrueType,
161 std::true_type>>::value));
162 EXPECT_TRUE((
163 std::is_base_of<MyFalseType,
164 absl::disjunction<std::false_type, MyFalseType>>::value));
165 }
166
TEST(NegationTest,BasicBooleanLogic)167 TEST(NegationTest, BasicBooleanLogic) {
168 EXPECT_FALSE(absl::negation<std::true_type>::value);
169 EXPECT_FALSE(absl::negation<MyTrueType>::value);
170 EXPECT_TRUE(absl::negation<std::false_type>::value);
171 EXPECT_TRUE(absl::negation<MyFalseType>::value);
172 }
173
174 // all member functions are trivial
175 class Trivial {
176 int n_;
177 };
178
179 struct TrivialDestructor {
180 ~TrivialDestructor() = default;
181 };
182
183 struct NontrivialDestructor {
~NontrivialDestructor__anonf6a731390111::NontrivialDestructor184 ~NontrivialDestructor() {}
185 };
186
187 struct DeletedDestructor {
188 ~DeletedDestructor() = delete;
189 };
190
191 class TrivialDefaultCtor {
192 public:
193 TrivialDefaultCtor() = default;
TrivialDefaultCtor(int n)194 explicit TrivialDefaultCtor(int n) : n_(n) {}
195
196 private:
197 int n_;
198 };
199
200 class NontrivialDefaultCtor {
201 public:
NontrivialDefaultCtor()202 NontrivialDefaultCtor() : n_(1) {}
203
204 private:
205 int n_;
206 };
207
208 class DeletedDefaultCtor {
209 public:
210 DeletedDefaultCtor() = delete;
DeletedDefaultCtor(int n)211 explicit DeletedDefaultCtor(int n) : n_(n) {}
212
213 private:
214 int n_;
215 };
216
217 class TrivialMoveCtor {
218 public:
TrivialMoveCtor(int n)219 explicit TrivialMoveCtor(int n) : n_(n) {}
220 TrivialMoveCtor(TrivialMoveCtor&&) = default;
operator =(const TrivialMoveCtor & t)221 TrivialMoveCtor& operator=(const TrivialMoveCtor& t) {
222 n_ = t.n_;
223 return *this;
224 }
225
226 private:
227 int n_;
228 };
229
230 class NontrivialMoveCtor {
231 public:
NontrivialMoveCtor(int n)232 explicit NontrivialMoveCtor(int n) : n_(n) {}
NontrivialMoveCtor(NontrivialMoveCtor && t)233 NontrivialMoveCtor(NontrivialMoveCtor&& t) noexcept : n_(t.n_) {}
234 NontrivialMoveCtor& operator=(const NontrivialMoveCtor&) = default;
235
236 private:
237 int n_;
238 };
239
240 class TrivialCopyCtor {
241 public:
TrivialCopyCtor(int n)242 explicit TrivialCopyCtor(int n) : n_(n) {}
243 TrivialCopyCtor(const TrivialCopyCtor&) = default;
operator =(const TrivialCopyCtor & t)244 TrivialCopyCtor& operator=(const TrivialCopyCtor& t) {
245 n_ = t.n_;
246 return *this;
247 }
248
249 private:
250 int n_;
251 };
252
253 class NontrivialCopyCtor {
254 public:
NontrivialCopyCtor(int n)255 explicit NontrivialCopyCtor(int n) : n_(n) {}
NontrivialCopyCtor(const NontrivialCopyCtor & t)256 NontrivialCopyCtor(const NontrivialCopyCtor& t) : n_(t.n_) {}
257 NontrivialCopyCtor& operator=(const NontrivialCopyCtor&) = default;
258
259 private:
260 int n_;
261 };
262
263 class DeletedCopyCtor {
264 public:
DeletedCopyCtor(int n)265 explicit DeletedCopyCtor(int n) : n_(n) {}
266 DeletedCopyCtor(const DeletedCopyCtor&) = delete;
267 DeletedCopyCtor& operator=(const DeletedCopyCtor&) = default;
268
269 private:
270 int n_;
271 };
272
273 class TrivialMoveAssign {
274 public:
TrivialMoveAssign(int n)275 explicit TrivialMoveAssign(int n) : n_(n) {}
TrivialMoveAssign(const TrivialMoveAssign & t)276 TrivialMoveAssign(const TrivialMoveAssign& t) : n_(t.n_) {}
277 TrivialMoveAssign& operator=(TrivialMoveAssign&&) = default;
~TrivialMoveAssign()278 ~TrivialMoveAssign() {} // can have nontrivial destructor
279 private:
280 int n_;
281 };
282
283 class NontrivialMoveAssign {
284 public:
NontrivialMoveAssign(int n)285 explicit NontrivialMoveAssign(int n) : n_(n) {}
286 NontrivialMoveAssign(const NontrivialMoveAssign&) = default;
operator =(NontrivialMoveAssign && t)287 NontrivialMoveAssign& operator=(NontrivialMoveAssign&& t) noexcept {
288 n_ = t.n_;
289 return *this;
290 }
291
292 private:
293 int n_;
294 };
295
296 class TrivialCopyAssign {
297 public:
TrivialCopyAssign(int n)298 explicit TrivialCopyAssign(int n) : n_(n) {}
TrivialCopyAssign(const TrivialCopyAssign & t)299 TrivialCopyAssign(const TrivialCopyAssign& t) : n_(t.n_) {}
300 TrivialCopyAssign& operator=(const TrivialCopyAssign& t) = default;
~TrivialCopyAssign()301 ~TrivialCopyAssign() {} // can have nontrivial destructor
302 private:
303 int n_;
304 };
305
306 class NontrivialCopyAssign {
307 public:
NontrivialCopyAssign(int n)308 explicit NontrivialCopyAssign(int n) : n_(n) {}
309 NontrivialCopyAssign(const NontrivialCopyAssign&) = default;
operator =(const NontrivialCopyAssign & t)310 NontrivialCopyAssign& operator=(const NontrivialCopyAssign& t) {
311 n_ = t.n_;
312 return *this;
313 }
314
315 private:
316 int n_;
317 };
318
319 class DeletedCopyAssign {
320 public:
DeletedCopyAssign(int n)321 explicit DeletedCopyAssign(int n) : n_(n) {}
322 DeletedCopyAssign(const DeletedCopyAssign&) = default;
323 DeletedCopyAssign& operator=(const DeletedCopyAssign&) = delete;
324
325 private:
326 int n_;
327 };
328
329 struct MovableNonCopyable {
330 MovableNonCopyable() = default;
331 MovableNonCopyable(const MovableNonCopyable&) = delete;
332 MovableNonCopyable(MovableNonCopyable&&) = default;
333 MovableNonCopyable& operator=(const MovableNonCopyable&) = delete;
334 MovableNonCopyable& operator=(MovableNonCopyable&&) = default;
335 };
336
337 struct NonCopyableOrMovable {
338 NonCopyableOrMovable() = default;
339 NonCopyableOrMovable(const NonCopyableOrMovable&) = delete;
340 NonCopyableOrMovable(NonCopyableOrMovable&&) = delete;
341 NonCopyableOrMovable& operator=(const NonCopyableOrMovable&) = delete;
342 NonCopyableOrMovable& operator=(NonCopyableOrMovable&&) = delete;
343 };
344
345 class Base {
346 public:
~Base()347 virtual ~Base() {}
348 };
349
350 // Old versions of libc++, around Clang 3.5 to 3.6, consider deleted destructors
351 // as also being trivial. With the resolution of CWG 1928 and CWG 1734, this
352 // is no longer considered true and has thus been amended.
353 // Compiler Explorer: https://godbolt.org/g/zT59ZL
354 // CWG issue 1734: http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1734
355 // CWG issue 1928: http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1928
356 #if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 3700
357 #define ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL 1
358 #endif
359
360 // As of the moment, GCC versions >5.1 have a problem compiling for
361 // std::is_trivially_default_constructible<NontrivialDestructor[10]>, where
362 // NontrivialDestructor is a struct with a custom nontrivial destructor. Note
363 // that this problem only occurs for arrays of a known size, so something like
364 // std::is_trivially_default_constructible<NontrivialDestructor[]> does not
365 // have any problems.
366 // Compiler Explorer: https://godbolt.org/g/dXRbdK
367 // GCC bug 83689: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83689
368 #if defined(__clang__) || defined(_MSC_VER) || \
369 (defined(__GNUC__) && __GNUC__ < 5)
370 #define ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL 1
371 #endif
372
TEST(TypeTraitsTest,TestIsFunction)373 TEST(TypeTraitsTest, TestIsFunction) {
374 struct Callable {
375 void operator()() {}
376 };
377 EXPECT_TRUE(absl::is_function<void()>::value);
378 EXPECT_TRUE(absl::is_function<void()&>::value);
379 EXPECT_TRUE(absl::is_function<void() const>::value);
380 EXPECT_TRUE(absl::is_function<void() noexcept>::value);
381 EXPECT_TRUE(absl::is_function<void(...) noexcept>::value);
382
383 EXPECT_FALSE(absl::is_function<void(*)()>::value);
384 EXPECT_FALSE(absl::is_function<void(&)()>::value);
385 EXPECT_FALSE(absl::is_function<int>::value);
386 EXPECT_FALSE(absl::is_function<Callable>::value);
387 }
388
TEST(TypeTraitsTest,TestTrivialDestructor)389 TEST(TypeTraitsTest, TestTrivialDestructor) {
390 // Verify that arithmetic types and pointers have trivial destructors.
391 EXPECT_TRUE(absl::is_trivially_destructible<bool>::value);
392 EXPECT_TRUE(absl::is_trivially_destructible<char>::value);
393 EXPECT_TRUE(absl::is_trivially_destructible<unsigned char>::value);
394 EXPECT_TRUE(absl::is_trivially_destructible<signed char>::value);
395 EXPECT_TRUE(absl::is_trivially_destructible<wchar_t>::value);
396 EXPECT_TRUE(absl::is_trivially_destructible<int>::value);
397 EXPECT_TRUE(absl::is_trivially_destructible<unsigned int>::value);
398 EXPECT_TRUE(absl::is_trivially_destructible<int16_t>::value);
399 EXPECT_TRUE(absl::is_trivially_destructible<uint16_t>::value);
400 EXPECT_TRUE(absl::is_trivially_destructible<int64_t>::value);
401 EXPECT_TRUE(absl::is_trivially_destructible<uint64_t>::value);
402 EXPECT_TRUE(absl::is_trivially_destructible<float>::value);
403 EXPECT_TRUE(absl::is_trivially_destructible<double>::value);
404 EXPECT_TRUE(absl::is_trivially_destructible<long double>::value);
405 EXPECT_TRUE(absl::is_trivially_destructible<std::string*>::value);
406 EXPECT_TRUE(absl::is_trivially_destructible<Trivial*>::value);
407 EXPECT_TRUE(absl::is_trivially_destructible<const std::string*>::value);
408 EXPECT_TRUE(absl::is_trivially_destructible<const Trivial*>::value);
409 EXPECT_TRUE(absl::is_trivially_destructible<std::string**>::value);
410 EXPECT_TRUE(absl::is_trivially_destructible<Trivial**>::value);
411
412 // classes with destructors
413 EXPECT_TRUE(absl::is_trivially_destructible<Trivial>::value);
414 EXPECT_TRUE(absl::is_trivially_destructible<TrivialDestructor>::value);
415
416 // Verify that types with a nontrivial or deleted destructor
417 // are marked as such.
418 EXPECT_FALSE(absl::is_trivially_destructible<NontrivialDestructor>::value);
419 #ifdef ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL
420 EXPECT_FALSE(absl::is_trivially_destructible<DeletedDestructor>::value);
421 #endif
422
423 // simple_pair of such types is trivial
424 EXPECT_TRUE((absl::is_trivially_destructible<simple_pair<int, int>>::value));
425 EXPECT_TRUE((absl::is_trivially_destructible<
426 simple_pair<Trivial, TrivialDestructor>>::value));
427
428 // Verify that types without trivial destructors are correctly marked as such.
429 EXPECT_FALSE(absl::is_trivially_destructible<std::string>::value);
430 EXPECT_FALSE(absl::is_trivially_destructible<std::vector<int>>::value);
431
432 // Verify that simple_pairs of types without trivial destructors
433 // are not marked as trivial.
434 EXPECT_FALSE((absl::is_trivially_destructible<
435 simple_pair<int, std::string>>::value));
436 EXPECT_FALSE((absl::is_trivially_destructible<
437 simple_pair<std::string, int>>::value));
438
439 // array of such types is trivial
440 using int10 = int[10];
441 EXPECT_TRUE(absl::is_trivially_destructible<int10>::value);
442 using Trivial10 = Trivial[10];
443 EXPECT_TRUE(absl::is_trivially_destructible<Trivial10>::value);
444 using TrivialDestructor10 = TrivialDestructor[10];
445 EXPECT_TRUE(absl::is_trivially_destructible<TrivialDestructor10>::value);
446
447 // Conversely, the opposite also holds.
448 using NontrivialDestructor10 = NontrivialDestructor[10];
449 EXPECT_FALSE(absl::is_trivially_destructible<NontrivialDestructor10>::value);
450 }
451
TEST(TypeTraitsTest,TestTrivialDefaultCtor)452 TEST(TypeTraitsTest, TestTrivialDefaultCtor) {
453 // arithmetic types and pointers have trivial default constructors.
454 EXPECT_TRUE(absl::is_trivially_default_constructible<bool>::value);
455 EXPECT_TRUE(absl::is_trivially_default_constructible<char>::value);
456 EXPECT_TRUE(absl::is_trivially_default_constructible<unsigned char>::value);
457 EXPECT_TRUE(absl::is_trivially_default_constructible<signed char>::value);
458 EXPECT_TRUE(absl::is_trivially_default_constructible<wchar_t>::value);
459 EXPECT_TRUE(absl::is_trivially_default_constructible<int>::value);
460 EXPECT_TRUE(absl::is_trivially_default_constructible<unsigned int>::value);
461 EXPECT_TRUE(absl::is_trivially_default_constructible<int16_t>::value);
462 EXPECT_TRUE(absl::is_trivially_default_constructible<uint16_t>::value);
463 EXPECT_TRUE(absl::is_trivially_default_constructible<int64_t>::value);
464 EXPECT_TRUE(absl::is_trivially_default_constructible<uint64_t>::value);
465 EXPECT_TRUE(absl::is_trivially_default_constructible<float>::value);
466 EXPECT_TRUE(absl::is_trivially_default_constructible<double>::value);
467 EXPECT_TRUE(absl::is_trivially_default_constructible<long double>::value);
468 EXPECT_TRUE(absl::is_trivially_default_constructible<std::string*>::value);
469 EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial*>::value);
470 EXPECT_TRUE(
471 absl::is_trivially_default_constructible<const std::string*>::value);
472 EXPECT_TRUE(absl::is_trivially_default_constructible<const Trivial*>::value);
473 EXPECT_TRUE(absl::is_trivially_default_constructible<std::string**>::value);
474 EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial**>::value);
475
476 // types with compiler generated default ctors
477 EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial>::value);
478 EXPECT_TRUE(
479 absl::is_trivially_default_constructible<TrivialDefaultCtor>::value);
480
481 // Verify that types without them are not.
482 EXPECT_FALSE(
483 absl::is_trivially_default_constructible<NontrivialDefaultCtor>::value);
484 EXPECT_FALSE(
485 absl::is_trivially_default_constructible<DeletedDefaultCtor>::value);
486
487 // types with nontrivial destructor are nontrivial
488 EXPECT_FALSE(
489 absl::is_trivially_default_constructible<NontrivialDestructor>::value);
490
491 // types with vtables
492 EXPECT_FALSE(absl::is_trivially_default_constructible<Base>::value);
493
494 // Verify that simple_pair has trivial constructors where applicable.
495 EXPECT_TRUE((absl::is_trivially_default_constructible<
496 simple_pair<int, char*>>::value));
497 EXPECT_TRUE((absl::is_trivially_default_constructible<
498 simple_pair<int, Trivial>>::value));
499 EXPECT_TRUE((absl::is_trivially_default_constructible<
500 simple_pair<int, TrivialDefaultCtor>>::value));
501
502 // Verify that types without trivial constructors are
503 // correctly marked as such.
504 EXPECT_FALSE(absl::is_trivially_default_constructible<std::string>::value);
505 EXPECT_FALSE(
506 absl::is_trivially_default_constructible<std::vector<int>>::value);
507
508 // Verify that simple_pairs of types without trivial constructors
509 // are not marked as trivial.
510 EXPECT_FALSE((absl::is_trivially_default_constructible<
511 simple_pair<int, std::string>>::value));
512 EXPECT_FALSE((absl::is_trivially_default_constructible<
513 simple_pair<std::string, int>>::value));
514
515 // Verify that arrays of such types are trivially default constructible
516 using int10 = int[10];
517 EXPECT_TRUE(absl::is_trivially_default_constructible<int10>::value);
518 using Trivial10 = Trivial[10];
519 EXPECT_TRUE(absl::is_trivially_default_constructible<Trivial10>::value);
520 using TrivialDefaultCtor10 = TrivialDefaultCtor[10];
521 EXPECT_TRUE(
522 absl::is_trivially_default_constructible<TrivialDefaultCtor10>::value);
523
524 // Conversely, the opposite also holds.
525 #ifdef ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL
526 using NontrivialDefaultCtor10 = NontrivialDefaultCtor[10];
527 EXPECT_FALSE(
528 absl::is_trivially_default_constructible<NontrivialDefaultCtor10>::value);
529 #endif
530 }
531
532 // GCC prior to 7.4 had a bug in its trivially-constructible traits
533 // (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654).
534 // This test makes sure that we do not depend on the trait in these cases when
535 // implementing absl triviality traits.
536
537 template <class T>
538 struct BadConstructors {
BadConstructors__anonf6a731390111::BadConstructors539 BadConstructors() { static_assert(T::value, ""); }
540
BadConstructors__anonf6a731390111::BadConstructors541 BadConstructors(BadConstructors&&) { static_assert(T::value, ""); }
542
BadConstructors__anonf6a731390111::BadConstructors543 BadConstructors(const BadConstructors&) { static_assert(T::value, ""); }
544 };
545
TEST(TypeTraitsTest,TestTrivialityBadConstructors)546 TEST(TypeTraitsTest, TestTrivialityBadConstructors) {
547 using BadType = BadConstructors<int>;
548
549 EXPECT_FALSE(absl::is_trivially_default_constructible<BadType>::value);
550 EXPECT_FALSE(absl::is_trivially_move_constructible<BadType>::value);
551 EXPECT_FALSE(absl::is_trivially_copy_constructible<BadType>::value);
552 }
553
TEST(TypeTraitsTest,TestTrivialMoveCtor)554 TEST(TypeTraitsTest, TestTrivialMoveCtor) {
555 // Verify that arithmetic types and pointers have trivial move
556 // constructors.
557 EXPECT_TRUE(absl::is_trivially_move_constructible<bool>::value);
558 EXPECT_TRUE(absl::is_trivially_move_constructible<char>::value);
559 EXPECT_TRUE(absl::is_trivially_move_constructible<unsigned char>::value);
560 EXPECT_TRUE(absl::is_trivially_move_constructible<signed char>::value);
561 EXPECT_TRUE(absl::is_trivially_move_constructible<wchar_t>::value);
562 EXPECT_TRUE(absl::is_trivially_move_constructible<int>::value);
563 EXPECT_TRUE(absl::is_trivially_move_constructible<unsigned int>::value);
564 EXPECT_TRUE(absl::is_trivially_move_constructible<int16_t>::value);
565 EXPECT_TRUE(absl::is_trivially_move_constructible<uint16_t>::value);
566 EXPECT_TRUE(absl::is_trivially_move_constructible<int64_t>::value);
567 EXPECT_TRUE(absl::is_trivially_move_constructible<uint64_t>::value);
568 EXPECT_TRUE(absl::is_trivially_move_constructible<float>::value);
569 EXPECT_TRUE(absl::is_trivially_move_constructible<double>::value);
570 EXPECT_TRUE(absl::is_trivially_move_constructible<long double>::value);
571 EXPECT_TRUE(absl::is_trivially_move_constructible<std::string*>::value);
572 EXPECT_TRUE(absl::is_trivially_move_constructible<Trivial*>::value);
573 EXPECT_TRUE(absl::is_trivially_move_constructible<const std::string*>::value);
574 EXPECT_TRUE(absl::is_trivially_move_constructible<const Trivial*>::value);
575 EXPECT_TRUE(absl::is_trivially_move_constructible<std::string**>::value);
576 EXPECT_TRUE(absl::is_trivially_move_constructible<Trivial**>::value);
577
578 // Reference types
579 EXPECT_TRUE(absl::is_trivially_move_constructible<int&>::value);
580 EXPECT_TRUE(absl::is_trivially_move_constructible<int&&>::value);
581
582 // types with compiler generated move ctors
583 EXPECT_TRUE(absl::is_trivially_move_constructible<Trivial>::value);
584 EXPECT_TRUE(absl::is_trivially_move_constructible<TrivialMoveCtor>::value);
585
586 // Verify that types without them (i.e. nontrivial or deleted) are not.
587 EXPECT_FALSE(
588 absl::is_trivially_move_constructible<NontrivialCopyCtor>::value);
589 EXPECT_FALSE(absl::is_trivially_move_constructible<DeletedCopyCtor>::value);
590 EXPECT_FALSE(
591 absl::is_trivially_move_constructible<NonCopyableOrMovable>::value);
592
593 // type with nontrivial destructor are nontrivial move construbtible
594 EXPECT_FALSE(
595 absl::is_trivially_move_constructible<NontrivialDestructor>::value);
596
597 // types with vtables
598 EXPECT_FALSE(absl::is_trivially_move_constructible<Base>::value);
599
600 // Verify that simple_pair of such types is trivially move constructible
601 EXPECT_TRUE(
602 (absl::is_trivially_move_constructible<simple_pair<int, char*>>::value));
603 EXPECT_TRUE((
604 absl::is_trivially_move_constructible<simple_pair<int, Trivial>>::value));
605 EXPECT_TRUE((absl::is_trivially_move_constructible<
606 simple_pair<int, TrivialMoveCtor>>::value));
607
608 // Verify that types without trivial move constructors are
609 // correctly marked as such.
610 EXPECT_FALSE(absl::is_trivially_move_constructible<std::string>::value);
611 EXPECT_FALSE(absl::is_trivially_move_constructible<std::vector<int>>::value);
612
613 // Verify that simple_pairs of types without trivial move constructors
614 // are not marked as trivial.
615 EXPECT_FALSE((absl::is_trivially_move_constructible<
616 simple_pair<int, std::string>>::value));
617 EXPECT_FALSE((absl::is_trivially_move_constructible<
618 simple_pair<std::string, int>>::value));
619
620 // Verify that arrays are not
621 using int10 = int[10];
622 EXPECT_FALSE(absl::is_trivially_move_constructible<int10>::value);
623 }
624
TEST(TypeTraitsTest,TestTrivialCopyCtor)625 TEST(TypeTraitsTest, TestTrivialCopyCtor) {
626 // Verify that arithmetic types and pointers have trivial copy
627 // constructors.
628 EXPECT_TRUE(absl::is_trivially_copy_constructible<bool>::value);
629 EXPECT_TRUE(absl::is_trivially_copy_constructible<char>::value);
630 EXPECT_TRUE(absl::is_trivially_copy_constructible<unsigned char>::value);
631 EXPECT_TRUE(absl::is_trivially_copy_constructible<signed char>::value);
632 EXPECT_TRUE(absl::is_trivially_copy_constructible<wchar_t>::value);
633 EXPECT_TRUE(absl::is_trivially_copy_constructible<int>::value);
634 EXPECT_TRUE(absl::is_trivially_copy_constructible<unsigned int>::value);
635 EXPECT_TRUE(absl::is_trivially_copy_constructible<int16_t>::value);
636 EXPECT_TRUE(absl::is_trivially_copy_constructible<uint16_t>::value);
637 EXPECT_TRUE(absl::is_trivially_copy_constructible<int64_t>::value);
638 EXPECT_TRUE(absl::is_trivially_copy_constructible<uint64_t>::value);
639 EXPECT_TRUE(absl::is_trivially_copy_constructible<float>::value);
640 EXPECT_TRUE(absl::is_trivially_copy_constructible<double>::value);
641 EXPECT_TRUE(absl::is_trivially_copy_constructible<long double>::value);
642 EXPECT_TRUE(absl::is_trivially_copy_constructible<std::string*>::value);
643 EXPECT_TRUE(absl::is_trivially_copy_constructible<Trivial*>::value);
644 EXPECT_TRUE(absl::is_trivially_copy_constructible<const std::string*>::value);
645 EXPECT_TRUE(absl::is_trivially_copy_constructible<const Trivial*>::value);
646 EXPECT_TRUE(absl::is_trivially_copy_constructible<std::string**>::value);
647 EXPECT_TRUE(absl::is_trivially_copy_constructible<Trivial**>::value);
648
649 // Reference types
650 EXPECT_TRUE(absl::is_trivially_copy_constructible<int&>::value);
651 EXPECT_FALSE(absl::is_trivially_copy_constructible<int&&>::value);
652
653 // types with compiler generated copy ctors
654 EXPECT_TRUE(absl::is_trivially_copy_constructible<Trivial>::value);
655 EXPECT_TRUE(absl::is_trivially_copy_constructible<TrivialCopyCtor>::value);
656
657 // Verify that types without them (i.e. nontrivial or deleted) are not.
658 EXPECT_FALSE(
659 absl::is_trivially_copy_constructible<NontrivialCopyCtor>::value);
660 EXPECT_FALSE(absl::is_trivially_copy_constructible<DeletedCopyCtor>::value);
661 EXPECT_FALSE(
662 absl::is_trivially_copy_constructible<MovableNonCopyable>::value);
663 EXPECT_FALSE(
664 absl::is_trivially_copy_constructible<NonCopyableOrMovable>::value);
665
666 // type with nontrivial destructor are nontrivial copy construbtible
667 EXPECT_FALSE(
668 absl::is_trivially_copy_constructible<NontrivialDestructor>::value);
669
670 // types with vtables
671 EXPECT_FALSE(absl::is_trivially_copy_constructible<Base>::value);
672
673 // Verify that simple_pair of such types is trivially copy constructible
674 EXPECT_TRUE(
675 (absl::is_trivially_copy_constructible<simple_pair<int, char*>>::value));
676 EXPECT_TRUE((
677 absl::is_trivially_copy_constructible<simple_pair<int, Trivial>>::value));
678 EXPECT_TRUE((absl::is_trivially_copy_constructible<
679 simple_pair<int, TrivialCopyCtor>>::value));
680
681 // Verify that types without trivial copy constructors are
682 // correctly marked as such.
683 EXPECT_FALSE(absl::is_trivially_copy_constructible<std::string>::value);
684 EXPECT_FALSE(absl::is_trivially_copy_constructible<std::vector<int>>::value);
685
686 // Verify that simple_pairs of types without trivial copy constructors
687 // are not marked as trivial.
688 EXPECT_FALSE((absl::is_trivially_copy_constructible<
689 simple_pair<int, std::string>>::value));
690 EXPECT_FALSE((absl::is_trivially_copy_constructible<
691 simple_pair<std::string, int>>::value));
692
693 // Verify that arrays are not
694 using int10 = int[10];
695 EXPECT_FALSE(absl::is_trivially_copy_constructible<int10>::value);
696 }
697
TEST(TypeTraitsTest,TestTrivialMoveAssign)698 TEST(TypeTraitsTest, TestTrivialMoveAssign) {
699 // Verify that arithmetic types and pointers have trivial move
700 // assignment operators.
701 EXPECT_TRUE(absl::is_trivially_move_assignable<bool>::value);
702 EXPECT_TRUE(absl::is_trivially_move_assignable<char>::value);
703 EXPECT_TRUE(absl::is_trivially_move_assignable<unsigned char>::value);
704 EXPECT_TRUE(absl::is_trivially_move_assignable<signed char>::value);
705 EXPECT_TRUE(absl::is_trivially_move_assignable<wchar_t>::value);
706 EXPECT_TRUE(absl::is_trivially_move_assignable<int>::value);
707 EXPECT_TRUE(absl::is_trivially_move_assignable<unsigned int>::value);
708 EXPECT_TRUE(absl::is_trivially_move_assignable<int16_t>::value);
709 EXPECT_TRUE(absl::is_trivially_move_assignable<uint16_t>::value);
710 EXPECT_TRUE(absl::is_trivially_move_assignable<int64_t>::value);
711 EXPECT_TRUE(absl::is_trivially_move_assignable<uint64_t>::value);
712 EXPECT_TRUE(absl::is_trivially_move_assignable<float>::value);
713 EXPECT_TRUE(absl::is_trivially_move_assignable<double>::value);
714 EXPECT_TRUE(absl::is_trivially_move_assignable<long double>::value);
715 EXPECT_TRUE(absl::is_trivially_move_assignable<std::string*>::value);
716 EXPECT_TRUE(absl::is_trivially_move_assignable<Trivial*>::value);
717 EXPECT_TRUE(absl::is_trivially_move_assignable<const std::string*>::value);
718 EXPECT_TRUE(absl::is_trivially_move_assignable<const Trivial*>::value);
719 EXPECT_TRUE(absl::is_trivially_move_assignable<std::string**>::value);
720 EXPECT_TRUE(absl::is_trivially_move_assignable<Trivial**>::value);
721
722 // const qualified types are not assignable
723 EXPECT_FALSE(absl::is_trivially_move_assignable<const int>::value);
724
725 // types with compiler generated move assignment
726 EXPECT_TRUE(absl::is_trivially_move_assignable<Trivial>::value);
727 EXPECT_TRUE(absl::is_trivially_move_assignable<TrivialMoveAssign>::value);
728
729 // Verify that types without them (i.e. nontrivial or deleted) are not.
730 EXPECT_FALSE(absl::is_trivially_move_assignable<NontrivialCopyAssign>::value);
731 EXPECT_FALSE(absl::is_trivially_move_assignable<DeletedCopyAssign>::value);
732 EXPECT_FALSE(absl::is_trivially_move_assignable<NonCopyableOrMovable>::value);
733
734 // types with vtables
735 EXPECT_FALSE(absl::is_trivially_move_assignable<Base>::value);
736
737 // Verify that simple_pair is trivially assignable
738 EXPECT_TRUE(
739 (absl::is_trivially_move_assignable<simple_pair<int, char*>>::value));
740 EXPECT_TRUE(
741 (absl::is_trivially_move_assignable<simple_pair<int, Trivial>>::value));
742 EXPECT_TRUE((absl::is_trivially_move_assignable<
743 simple_pair<int, TrivialMoveAssign>>::value));
744
745 // Verify that types not trivially move assignable are
746 // correctly marked as such.
747 EXPECT_FALSE(absl::is_trivially_move_assignable<std::string>::value);
748 EXPECT_FALSE(absl::is_trivially_move_assignable<std::vector<int>>::value);
749
750 // Verify that simple_pairs of types not trivially move assignable
751 // are not marked as trivial.
752 EXPECT_FALSE((absl::is_trivially_move_assignable<
753 simple_pair<int, std::string>>::value));
754 EXPECT_FALSE((absl::is_trivially_move_assignable<
755 simple_pair<std::string, int>>::value));
756
757 // Verify that arrays are not trivially move assignable
758 using int10 = int[10];
759 EXPECT_FALSE(absl::is_trivially_move_assignable<int10>::value);
760
761 // Verify that references are handled correctly
762 EXPECT_TRUE(absl::is_trivially_move_assignable<Trivial&&>::value);
763 EXPECT_TRUE(absl::is_trivially_move_assignable<Trivial&>::value);
764 }
765
TEST(TypeTraitsTest,TestTrivialCopyAssign)766 TEST(TypeTraitsTest, TestTrivialCopyAssign) {
767 // Verify that arithmetic types and pointers have trivial copy
768 // assignment operators.
769 EXPECT_TRUE(absl::is_trivially_copy_assignable<bool>::value);
770 EXPECT_TRUE(absl::is_trivially_copy_assignable<char>::value);
771 EXPECT_TRUE(absl::is_trivially_copy_assignable<unsigned char>::value);
772 EXPECT_TRUE(absl::is_trivially_copy_assignable<signed char>::value);
773 EXPECT_TRUE(absl::is_trivially_copy_assignable<wchar_t>::value);
774 EXPECT_TRUE(absl::is_trivially_copy_assignable<int>::value);
775 EXPECT_TRUE(absl::is_trivially_copy_assignable<unsigned int>::value);
776 EXPECT_TRUE(absl::is_trivially_copy_assignable<int16_t>::value);
777 EXPECT_TRUE(absl::is_trivially_copy_assignable<uint16_t>::value);
778 EXPECT_TRUE(absl::is_trivially_copy_assignable<int64_t>::value);
779 EXPECT_TRUE(absl::is_trivially_copy_assignable<uint64_t>::value);
780 EXPECT_TRUE(absl::is_trivially_copy_assignable<float>::value);
781 EXPECT_TRUE(absl::is_trivially_copy_assignable<double>::value);
782 EXPECT_TRUE(absl::is_trivially_copy_assignable<long double>::value);
783 EXPECT_TRUE(absl::is_trivially_copy_assignable<std::string*>::value);
784 EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial*>::value);
785 EXPECT_TRUE(absl::is_trivially_copy_assignable<const std::string*>::value);
786 EXPECT_TRUE(absl::is_trivially_copy_assignable<const Trivial*>::value);
787 EXPECT_TRUE(absl::is_trivially_copy_assignable<std::string**>::value);
788 EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial**>::value);
789
790 // const qualified types are not assignable
791 EXPECT_FALSE(absl::is_trivially_copy_assignable<const int>::value);
792
793 // types with compiler generated copy assignment
794 EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial>::value);
795 EXPECT_TRUE(absl::is_trivially_copy_assignable<TrivialCopyAssign>::value);
796
797 // Verify that types without them (i.e. nontrivial or deleted) are not.
798 EXPECT_FALSE(absl::is_trivially_copy_assignable<NontrivialCopyAssign>::value);
799 EXPECT_FALSE(absl::is_trivially_copy_assignable<DeletedCopyAssign>::value);
800 EXPECT_FALSE(absl::is_trivially_copy_assignable<MovableNonCopyable>::value);
801 EXPECT_FALSE(absl::is_trivially_copy_assignable<NonCopyableOrMovable>::value);
802
803 // types with vtables
804 EXPECT_FALSE(absl::is_trivially_copy_assignable<Base>::value);
805
806 // Verify that simple_pair is trivially assignable
807 EXPECT_TRUE(
808 (absl::is_trivially_copy_assignable<simple_pair<int, char*>>::value));
809 EXPECT_TRUE(
810 (absl::is_trivially_copy_assignable<simple_pair<int, Trivial>>::value));
811 EXPECT_TRUE((absl::is_trivially_copy_assignable<
812 simple_pair<int, TrivialCopyAssign>>::value));
813
814 // Verify that types not trivially copy assignable are
815 // correctly marked as such.
816 EXPECT_FALSE(absl::is_trivially_copy_assignable<std::string>::value);
817 EXPECT_FALSE(absl::is_trivially_copy_assignable<std::vector<int>>::value);
818
819 // Verify that simple_pairs of types not trivially copy assignable
820 // are not marked as trivial.
821 EXPECT_FALSE((absl::is_trivially_copy_assignable<
822 simple_pair<int, std::string>>::value));
823 EXPECT_FALSE((absl::is_trivially_copy_assignable<
824 simple_pair<std::string, int>>::value));
825
826 // Verify that arrays are not trivially copy assignable
827 using int10 = int[10];
828 EXPECT_FALSE(absl::is_trivially_copy_assignable<int10>::value);
829
830 // Verify that references are handled correctly
831 EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial&&>::value);
832 EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial&>::value);
833 }
834
TEST(TypeTraitsTest,TestTriviallyCopyable)835 TEST(TypeTraitsTest, TestTriviallyCopyable) {
836 // Verify that arithmetic types and pointers are trivially copyable.
837 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<bool>::value);
838 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<char>::value);
839 EXPECT_TRUE(
840 absl::type_traits_internal::is_trivially_copyable<unsigned char>::value);
841 EXPECT_TRUE(
842 absl::type_traits_internal::is_trivially_copyable<signed char>::value);
843 EXPECT_TRUE(
844 absl::type_traits_internal::is_trivially_copyable<wchar_t>::value);
845 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<int>::value);
846 EXPECT_TRUE(
847 absl::type_traits_internal::is_trivially_copyable<unsigned int>::value);
848 EXPECT_TRUE(
849 absl::type_traits_internal::is_trivially_copyable<int16_t>::value);
850 EXPECT_TRUE(
851 absl::type_traits_internal::is_trivially_copyable<uint16_t>::value);
852 EXPECT_TRUE(
853 absl::type_traits_internal::is_trivially_copyable<int64_t>::value);
854 EXPECT_TRUE(
855 absl::type_traits_internal::is_trivially_copyable<uint64_t>::value);
856 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<float>::value);
857 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<double>::value);
858 EXPECT_TRUE(
859 absl::type_traits_internal::is_trivially_copyable<long double>::value);
860 EXPECT_TRUE(
861 absl::type_traits_internal::is_trivially_copyable<std::string*>::value);
862 EXPECT_TRUE(
863 absl::type_traits_internal::is_trivially_copyable<Trivial*>::value);
864 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<
865 const std::string*>::value);
866 EXPECT_TRUE(
867 absl::type_traits_internal::is_trivially_copyable<const Trivial*>::value);
868 EXPECT_TRUE(
869 absl::type_traits_internal::is_trivially_copyable<std::string**>::value);
870 EXPECT_TRUE(
871 absl::type_traits_internal::is_trivially_copyable<Trivial**>::value);
872
873 // const qualified types are not assignable but are constructible
874 EXPECT_TRUE(
875 absl::type_traits_internal::is_trivially_copyable<const int>::value);
876
877 // Trivial copy constructor/assignment and destructor.
878 EXPECT_TRUE(
879 absl::type_traits_internal::is_trivially_copyable<Trivial>::value);
880 // Trivial copy assignment, but non-trivial copy constructor/destructor.
881 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<
882 TrivialCopyAssign>::value);
883 // Trivial copy constructor, but non-trivial assignment.
884 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<
885 TrivialCopyCtor>::value);
886
887 // Types with a non-trivial copy constructor/assignment
888 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<
889 NontrivialCopyCtor>::value);
890 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<
891 NontrivialCopyAssign>::value);
892
893 // Types without copy constructor/assignment, but with move
894 // MSVC disagrees with other compilers about this:
895 // EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<
896 // MovableNonCopyable>::value);
897
898 // Types without copy/move constructor/assignment
899 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<
900 NonCopyableOrMovable>::value);
901
902 // No copy assign, but has trivial copy constructor.
903 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<
904 DeletedCopyAssign>::value);
905
906 // types with vtables
907 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<Base>::value);
908
909 // Verify that simple_pair is trivially copyable if members are
910 EXPECT_TRUE((absl::type_traits_internal::is_trivially_copyable<
911 simple_pair<int, char*>>::value));
912 EXPECT_TRUE((absl::type_traits_internal::is_trivially_copyable<
913 simple_pair<int, Trivial>>::value));
914
915 // Verify that types not trivially copyable are
916 // correctly marked as such.
917 EXPECT_FALSE(
918 absl::type_traits_internal::is_trivially_copyable<std::string>::value);
919 EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable<
920 std::vector<int>>::value);
921
922 // Verify that simple_pairs of types not trivially copyable
923 // are not marked as trivial.
924 EXPECT_FALSE((absl::type_traits_internal::is_trivially_copyable<
925 simple_pair<int, std::string>>::value));
926 EXPECT_FALSE((absl::type_traits_internal::is_trivially_copyable<
927 simple_pair<std::string, int>>::value));
928 EXPECT_FALSE((absl::type_traits_internal::is_trivially_copyable<
929 simple_pair<int, TrivialCopyAssign>>::value));
930
931 // Verify that arrays of trivially copyable types are trivially copyable
932 using int10 = int[10];
933 EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable<int10>::value);
934 using int10x10 = int[10][10];
935 EXPECT_TRUE(
936 absl::type_traits_internal::is_trivially_copyable<int10x10>::value);
937
938 // Verify that references are handled correctly
939 EXPECT_FALSE(
940 absl::type_traits_internal::is_trivially_copyable<Trivial&&>::value);
941 EXPECT_FALSE(
942 absl::type_traits_internal::is_trivially_copyable<Trivial&>::value);
943 }
944
TEST(TypeTraitsTest,TestRemoveCVRef)945 TEST(TypeTraitsTest, TestRemoveCVRef) {
946 EXPECT_TRUE(
947 (std::is_same<typename absl::remove_cvref<int>::type, int>::value));
948 EXPECT_TRUE(
949 (std::is_same<typename absl::remove_cvref<int&>::type, int>::value));
950 EXPECT_TRUE(
951 (std::is_same<typename absl::remove_cvref<int&&>::type, int>::value));
952 EXPECT_TRUE((
953 std::is_same<typename absl::remove_cvref<const int&>::type, int>::value));
954 EXPECT_TRUE(
955 (std::is_same<typename absl::remove_cvref<int*>::type, int*>::value));
956 // Does not remove const in this case.
957 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<const int*>::type,
958 const int*>::value));
959 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<int[2]>::type,
960 int[2]>::value));
961 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<int(&)[2]>::type,
962 int[2]>::value));
963 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<int(&&)[2]>::type,
964 int[2]>::value));
965 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<const int[2]>::type,
966 int[2]>::value));
967 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<const int(&)[2]>::type,
968 int[2]>::value));
969 EXPECT_TRUE((std::is_same<typename absl::remove_cvref<const int(&&)[2]>::type,
970 int[2]>::value));
971 }
972
973 #define ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(trait_name, ...) \
974 EXPECT_TRUE((std::is_same<typename std::trait_name<__VA_ARGS__>::type, \
975 absl::trait_name##_t<__VA_ARGS__>>::value))
976
TEST(TypeTraitsTest,TestRemoveCVAliases)977 TEST(TypeTraitsTest, TestRemoveCVAliases) {
978 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_cv, int);
979 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_cv, const int);
980 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_cv, volatile int);
981 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_cv, const volatile int);
982
983 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_const, int);
984 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_const, const int);
985 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_const, volatile int);
986 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_const, const volatile int);
987
988 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_volatile, int);
989 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_volatile, const int);
990 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_volatile, volatile int);
991 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_volatile, const volatile int);
992 }
993
TEST(TypeTraitsTest,TestAddCVAliases)994 TEST(TypeTraitsTest, TestAddCVAliases) {
995 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_cv, int);
996 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_cv, const int);
997 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_cv, volatile int);
998 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_cv, const volatile int);
999
1000 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_const, int);
1001 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_const, const int);
1002 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_const, volatile int);
1003 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_const, const volatile int);
1004
1005 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_volatile, int);
1006 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_volatile, const int);
1007 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_volatile, volatile int);
1008 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_volatile, const volatile int);
1009 }
1010
TEST(TypeTraitsTest,TestReferenceAliases)1011 TEST(TypeTraitsTest, TestReferenceAliases) {
1012 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_reference, int);
1013 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_reference, volatile int);
1014 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_reference, int&);
1015 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_reference, volatile int&);
1016 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_reference, int&&);
1017 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_reference, volatile int&&);
1018
1019 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_lvalue_reference, int);
1020 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_lvalue_reference, volatile int);
1021 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_lvalue_reference, int&);
1022 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_lvalue_reference, volatile int&);
1023 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_lvalue_reference, int&&);
1024 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_lvalue_reference, volatile int&&);
1025
1026 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_rvalue_reference, int);
1027 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_rvalue_reference, volatile int);
1028 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_rvalue_reference, int&);
1029 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_rvalue_reference, volatile int&);
1030 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_rvalue_reference, int&&);
1031 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_rvalue_reference, volatile int&&);
1032 }
1033
TEST(TypeTraitsTest,TestPointerAliases)1034 TEST(TypeTraitsTest, TestPointerAliases) {
1035 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_pointer, int*);
1036 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_pointer, volatile int*);
1037
1038 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_pointer, int);
1039 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(add_pointer, volatile int);
1040 }
1041
TEST(TypeTraitsTest,TestSignednessAliases)1042 TEST(TypeTraitsTest, TestSignednessAliases) {
1043 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_signed, int);
1044 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_signed, volatile int);
1045 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_signed, unsigned);
1046 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_signed, volatile unsigned);
1047
1048 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_unsigned, int);
1049 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_unsigned, volatile int);
1050 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_unsigned, unsigned);
1051 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(make_unsigned, volatile unsigned);
1052 }
1053
TEST(TypeTraitsTest,TestExtentAliases)1054 TEST(TypeTraitsTest, TestExtentAliases) {
1055 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_extent, int[]);
1056 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_extent, int[1]);
1057 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_extent, int[1][1]);
1058 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_extent, int[][1]);
1059
1060 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_all_extents, int[]);
1061 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_all_extents, int[1]);
1062 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_all_extents, int[1][1]);
1063 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(remove_all_extents, int[][1]);
1064 }
1065
TEST(TypeTraitsTest,TestAlignedStorageAlias)1066 TEST(TypeTraitsTest, TestAlignedStorageAlias) {
1067 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 1);
1068 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 2);
1069 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 3);
1070 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 4);
1071 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 5);
1072 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 6);
1073 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 7);
1074 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 8);
1075 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 9);
1076 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 10);
1077 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 11);
1078 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 12);
1079 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 13);
1080 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 14);
1081 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 15);
1082 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 16);
1083 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 17);
1084 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 18);
1085 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 19);
1086 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 20);
1087 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 21);
1088 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 22);
1089 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 23);
1090 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 24);
1091 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 25);
1092 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 26);
1093 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 27);
1094 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 28);
1095 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 29);
1096 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 30);
1097 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 31);
1098 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 32);
1099 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 33);
1100
1101 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 1, 128);
1102 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 2, 128);
1103 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 3, 128);
1104 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 4, 128);
1105 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 5, 128);
1106 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 6, 128);
1107 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 7, 128);
1108 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 8, 128);
1109 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 9, 128);
1110 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 10, 128);
1111 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 11, 128);
1112 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 12, 128);
1113 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 13, 128);
1114 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 14, 128);
1115 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 15, 128);
1116 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 16, 128);
1117 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 17, 128);
1118 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 18, 128);
1119 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 19, 128);
1120 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 20, 128);
1121 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 21, 128);
1122 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 22, 128);
1123 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 23, 128);
1124 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 24, 128);
1125 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 25, 128);
1126 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 26, 128);
1127 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 27, 128);
1128 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 28, 128);
1129 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 29, 128);
1130 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 30, 128);
1131 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 31, 128);
1132 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 32, 128);
1133 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(aligned_storage, 33, 128);
1134 }
1135
TEST(TypeTraitsTest,TestDecay)1136 TEST(TypeTraitsTest, TestDecay) {
1137 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int);
1138 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, const int);
1139 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, volatile int);
1140 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, const volatile int);
1141
1142 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int&);
1143 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, const int&);
1144 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, volatile int&);
1145 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, const volatile int&);
1146
1147 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int&);
1148 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, const int&);
1149 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, volatile int&);
1150 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, const volatile int&);
1151
1152 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int[1]);
1153 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int[1][1]);
1154 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int[][1]);
1155
1156 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int());
1157 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(float)); // NOLINT
1158 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(decay, int(char, ...)); // NOLINT
1159 }
1160
1161 struct TypeA {};
1162 struct TypeB {};
1163 struct TypeC {};
1164 struct TypeD {};
1165
1166 template <typename T>
1167 struct Wrap {};
1168
1169 enum class TypeEnum { A, B, C, D };
1170
1171 struct GetTypeT {
1172 template <typename T,
1173 absl::enable_if_t<std::is_same<T, TypeA>::value, int> = 0>
operator ()__anonf6a731390111::GetTypeT1174 TypeEnum operator()(Wrap<T>) const {
1175 return TypeEnum::A;
1176 }
1177
1178 template <typename T,
1179 absl::enable_if_t<std::is_same<T, TypeB>::value, int> = 0>
operator ()__anonf6a731390111::GetTypeT1180 TypeEnum operator()(Wrap<T>) const {
1181 return TypeEnum::B;
1182 }
1183
1184 template <typename T,
1185 absl::enable_if_t<std::is_same<T, TypeC>::value, int> = 0>
operator ()__anonf6a731390111::GetTypeT1186 TypeEnum operator()(Wrap<T>) const {
1187 return TypeEnum::C;
1188 }
1189
1190 // NOTE: TypeD is intentionally not handled
1191 } constexpr GetType = {};
1192
TEST(TypeTraitsTest,TestEnableIf)1193 TEST(TypeTraitsTest, TestEnableIf) {
1194 EXPECT_EQ(TypeEnum::A, GetType(Wrap<TypeA>()));
1195 EXPECT_EQ(TypeEnum::B, GetType(Wrap<TypeB>()));
1196 EXPECT_EQ(TypeEnum::C, GetType(Wrap<TypeC>()));
1197 }
1198
TEST(TypeTraitsTest,TestConditional)1199 TEST(TypeTraitsTest, TestConditional) {
1200 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(conditional, true, int, char);
1201 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(conditional, false, int, char);
1202 }
1203
1204 // TODO(calabrese) Check with specialized std::common_type
TEST(TypeTraitsTest,TestCommonType)1205 TEST(TypeTraitsTest, TestCommonType) {
1206 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(common_type, int);
1207 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(common_type, int, char);
1208 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(common_type, int, char, int);
1209
1210 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(common_type, int&);
1211 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(common_type, int, char&);
1212 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(common_type, int, char, int&);
1213 }
1214
TEST(TypeTraitsTest,TestUnderlyingType)1215 TEST(TypeTraitsTest, TestUnderlyingType) {
1216 enum class enum_char : char {};
1217 enum class enum_long_long : long long {}; // NOLINT(runtime/int)
1218
1219 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(underlying_type, enum_char);
1220 ABSL_INTERNAL_EXPECT_ALIAS_EQUIVALENCE(underlying_type, enum_long_long);
1221 }
1222
1223 struct GetTypeExtT {
1224 template <typename T>
operator ()__anonf6a731390111::GetTypeExtT1225 absl::result_of_t<const GetTypeT&(T)> operator()(T&& arg) const {
1226 return GetType(std::forward<T>(arg));
1227 }
1228
operator ()__anonf6a731390111::GetTypeExtT1229 TypeEnum operator()(Wrap<TypeD>) const { return TypeEnum::D; }
1230 } constexpr GetTypeExt = {};
1231
TEST(TypeTraitsTest,TestResultOf)1232 TEST(TypeTraitsTest, TestResultOf) {
1233 EXPECT_EQ(TypeEnum::A, GetTypeExt(Wrap<TypeA>()));
1234 EXPECT_EQ(TypeEnum::B, GetTypeExt(Wrap<TypeB>()));
1235 EXPECT_EQ(TypeEnum::C, GetTypeExt(Wrap<TypeC>()));
1236 EXPECT_EQ(TypeEnum::D, GetTypeExt(Wrap<TypeD>()));
1237 }
1238
1239 template <typename T>
TestCopyAssign()1240 bool TestCopyAssign() {
1241 return absl::is_copy_assignable<T>::value ==
1242 std::is_copy_assignable<T>::value;
1243 }
1244
TEST(TypeTraitsTest,IsCopyAssignable)1245 TEST(TypeTraitsTest, IsCopyAssignable) {
1246 EXPECT_TRUE(TestCopyAssign<int>());
1247 EXPECT_TRUE(TestCopyAssign<int&>());
1248 EXPECT_TRUE(TestCopyAssign<int&&>());
1249
1250 struct S {};
1251 EXPECT_TRUE(TestCopyAssign<S>());
1252 EXPECT_TRUE(TestCopyAssign<S&>());
1253 EXPECT_TRUE(TestCopyAssign<S&&>());
1254
1255 class C {
1256 public:
1257 explicit C(C* c) : c_(c) {}
1258 ~C() { delete c_; }
1259
1260 private:
1261 C* c_;
1262 };
1263 EXPECT_TRUE(TestCopyAssign<C>());
1264 EXPECT_TRUE(TestCopyAssign<C&>());
1265 EXPECT_TRUE(TestCopyAssign<C&&>());
1266
1267 // Reason for ifndef: add_lvalue_reference<T> in libc++ breaks for these cases
1268 #ifndef _LIBCPP_VERSION
1269 EXPECT_TRUE(TestCopyAssign<int()>());
1270 EXPECT_TRUE(TestCopyAssign<int(int) const>());
1271 EXPECT_TRUE(TestCopyAssign<int(...) volatile&>());
1272 EXPECT_TRUE(TestCopyAssign<int(int, ...) const volatile&&>());
1273 #endif // _LIBCPP_VERSION
1274 }
1275
1276 template <typename T>
TestMoveAssign()1277 bool TestMoveAssign() {
1278 return absl::is_move_assignable<T>::value ==
1279 std::is_move_assignable<T>::value;
1280 }
1281
TEST(TypeTraitsTest,IsMoveAssignable)1282 TEST(TypeTraitsTest, IsMoveAssignable) {
1283 EXPECT_TRUE(TestMoveAssign<int>());
1284 EXPECT_TRUE(TestMoveAssign<int&>());
1285 EXPECT_TRUE(TestMoveAssign<int&&>());
1286
1287 struct S {};
1288 EXPECT_TRUE(TestMoveAssign<S>());
1289 EXPECT_TRUE(TestMoveAssign<S&>());
1290 EXPECT_TRUE(TestMoveAssign<S&&>());
1291
1292 class C {
1293 public:
1294 explicit C(C* c) : c_(c) {}
1295 ~C() { delete c_; }
1296 void operator=(const C&) = delete;
1297 void operator=(C&&) = delete;
1298
1299 private:
1300 C* c_;
1301 };
1302 EXPECT_TRUE(TestMoveAssign<C>());
1303 EXPECT_TRUE(TestMoveAssign<C&>());
1304 EXPECT_TRUE(TestMoveAssign<C&&>());
1305
1306 // Reason for ifndef: add_lvalue_reference<T> in libc++ breaks for these cases
1307 #ifndef _LIBCPP_VERSION
1308 EXPECT_TRUE(TestMoveAssign<int()>());
1309 EXPECT_TRUE(TestMoveAssign<int(int) const>());
1310 EXPECT_TRUE(TestMoveAssign<int(...) volatile&>());
1311 EXPECT_TRUE(TestMoveAssign<int(int, ...) const volatile&&>());
1312 #endif // _LIBCPP_VERSION
1313 }
1314
1315 namespace adl_namespace {
1316
1317 struct DeletedSwap {
1318 };
1319
1320 void swap(DeletedSwap&, DeletedSwap&) = delete;
1321
1322 struct SpecialNoexceptSwap {
SpecialNoexceptSwap__anonf6a731390111::adl_namespace::SpecialNoexceptSwap1323 SpecialNoexceptSwap(SpecialNoexceptSwap&&) {}
operator =__anonf6a731390111::adl_namespace::SpecialNoexceptSwap1324 SpecialNoexceptSwap& operator=(SpecialNoexceptSwap&&) { return *this; }
1325 ~SpecialNoexceptSwap() = default;
1326 };
1327
swap(SpecialNoexceptSwap &,SpecialNoexceptSwap &)1328 void swap(SpecialNoexceptSwap&, SpecialNoexceptSwap&) noexcept {}
1329
1330 } // namespace adl_namespace
1331
TEST(TypeTraitsTest,IsSwappable)1332 TEST(TypeTraitsTest, IsSwappable) {
1333 using absl::type_traits_internal::IsSwappable;
1334 using absl::type_traits_internal::StdSwapIsUnconstrained;
1335
1336 EXPECT_TRUE(IsSwappable<int>::value);
1337
1338 struct S {};
1339 EXPECT_TRUE(IsSwappable<S>::value);
1340
1341 struct NoConstruct {
1342 NoConstruct(NoConstruct&&) = delete;
1343 NoConstruct& operator=(NoConstruct&&) { return *this; }
1344 ~NoConstruct() = default;
1345 };
1346
1347 EXPECT_EQ(IsSwappable<NoConstruct>::value, StdSwapIsUnconstrained::value);
1348 struct NoAssign {
1349 NoAssign(NoAssign&&) {}
1350 NoAssign& operator=(NoAssign&&) = delete;
1351 ~NoAssign() = default;
1352 };
1353
1354 EXPECT_EQ(IsSwappable<NoAssign>::value, StdSwapIsUnconstrained::value);
1355
1356 EXPECT_FALSE(IsSwappable<adl_namespace::DeletedSwap>::value);
1357
1358 EXPECT_TRUE(IsSwappable<adl_namespace::SpecialNoexceptSwap>::value);
1359 }
1360
TEST(TypeTraitsTest,IsNothrowSwappable)1361 TEST(TypeTraitsTest, IsNothrowSwappable) {
1362 using absl::type_traits_internal::IsNothrowSwappable;
1363 using absl::type_traits_internal::StdSwapIsUnconstrained;
1364
1365 EXPECT_TRUE(IsNothrowSwappable<int>::value);
1366
1367 struct NonNoexceptMoves {
1368 NonNoexceptMoves(NonNoexceptMoves&&) {}
1369 NonNoexceptMoves& operator=(NonNoexceptMoves&&) { return *this; }
1370 ~NonNoexceptMoves() = default;
1371 };
1372
1373 EXPECT_FALSE(IsNothrowSwappable<NonNoexceptMoves>::value);
1374
1375 struct NoConstruct {
1376 NoConstruct(NoConstruct&&) = delete;
1377 NoConstruct& operator=(NoConstruct&&) { return *this; }
1378 ~NoConstruct() = default;
1379 };
1380
1381 EXPECT_FALSE(IsNothrowSwappable<NoConstruct>::value);
1382
1383 struct NoAssign {
1384 NoAssign(NoAssign&&) {}
1385 NoAssign& operator=(NoAssign&&) = delete;
1386 ~NoAssign() = default;
1387 };
1388
1389 EXPECT_FALSE(IsNothrowSwappable<NoAssign>::value);
1390
1391 EXPECT_FALSE(IsNothrowSwappable<adl_namespace::DeletedSwap>::value);
1392
1393 EXPECT_TRUE(IsNothrowSwappable<adl_namespace::SpecialNoexceptSwap>::value);
1394 }
1395
1396 } // namespace
1397