gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/template_util.h" |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace base { |
| 11 | namespace { |
| 12 | |
| 13 | struct AStruct {}; |
| 14 | class AClass {}; |
| 15 | enum AnEnum {}; |
| 16 | |
| 17 | class Parent {}; |
| 18 | class Child : public Parent {}; |
| 19 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 20 | // is_pointer<Type> |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 21 | static_assert(!is_pointer<int>::value, "IsPointer"); |
| 22 | static_assert(!is_pointer<int&>::value, "IsPointer"); |
| 23 | static_assert(is_pointer<int*>::value, "IsPointer"); |
| 24 | static_assert(is_pointer<const int*>::value, "IsPointer"); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 25 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 26 | // is_array<Type> |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 27 | static_assert(!is_array<int>::value, "IsArray"); |
| 28 | static_assert(!is_array<int*>::value, "IsArray"); |
| 29 | static_assert(!is_array<int (*)[3]>::value, "IsArray"); |
| 30 | static_assert(is_array<int[]>::value, "IsArray"); |
| 31 | static_assert(is_array<const int[]>::value, "IsArray"); |
| 32 | static_assert(is_array<int[3]>::value, "IsArray"); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 33 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 34 | // is_non_const_reference<Type> |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 35 | static_assert(!is_non_const_reference<int>::value, "IsNonConstReference"); |
| 36 | static_assert(!is_non_const_reference<const int&>::value, |
| 37 | "IsNonConstReference"); |
| 38 | static_assert(is_non_const_reference<int&>::value, "IsNonConstReference"); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 39 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 40 | // is_convertible<From, To> |
ajwong@chromium.org | d5f6111 | 2011-02-17 07:37:58 +0900 | [diff] [blame] | 41 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 42 | // Extra parens needed to make preprocessor macro parsing happy. Otherwise, |
| 43 | // it sees the equivalent of: |
| 44 | // |
| 45 | // (is_convertible < Child), (Parent > ::value) |
| 46 | // |
| 47 | // Silly C++. |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 48 | static_assert((is_convertible<Child, Parent>::value), "IsConvertible"); |
| 49 | static_assert(!(is_convertible<Parent, Child>::value), "IsConvertible"); |
| 50 | static_assert(!(is_convertible<Parent, AStruct>::value), "IsConvertible"); |
| 51 | static_assert((is_convertible<int, double>::value), "IsConvertible"); |
| 52 | static_assert((is_convertible<int*, void*>::value), "IsConvertible"); |
| 53 | static_assert(!(is_convertible<void*, int*>::value), "IsConvertible"); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 54 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 55 | // Array types are an easy corner case. Make sure to test that |
| 56 | // it does indeed compile. |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 57 | static_assert(!(is_convertible<int[10], double>::value), "IsConvertible"); |
| 58 | static_assert(!(is_convertible<double, int[10]>::value), "IsConvertible"); |
| 59 | static_assert((is_convertible<int[10], int*>::value), "IsConvertible"); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 60 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 61 | // is_same<Type1, Type2> |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 62 | static_assert(!(is_same<Child, Parent>::value), "IsSame"); |
| 63 | static_assert(!(is_same<Parent, Child>::value), "IsSame"); |
| 64 | static_assert((is_same<Parent, Parent>::value), "IsSame"); |
| 65 | static_assert((is_same<int*, int*>::value), "IsSame"); |
| 66 | static_assert((is_same<int, int>::value), "IsSame"); |
| 67 | static_assert((is_same<void, void>::value), "IsSame"); |
| 68 | static_assert(!(is_same<int, double>::value), "IsSame"); |
ajwong@chromium.org | ec1750a | 2011-06-27 01:22:50 +0900 | [diff] [blame] | 69 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 70 | // is_class<Type> |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 71 | static_assert(is_class<AStruct>::value, "IsClass"); |
| 72 | static_assert(is_class<AClass>::value, "IsClass"); |
| 73 | static_assert(!is_class<AnEnum>::value, "IsClass"); |
| 74 | static_assert(!is_class<int>::value, "IsClass"); |
| 75 | static_assert(!is_class<char*>::value, "IsClass"); |
| 76 | static_assert(!is_class<int&>::value, "IsClass"); |
| 77 | static_assert(!is_class<char[3]>::value, "IsClass"); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 78 | |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 79 | static_assert(!is_member_function_pointer<int>::value, |
| 80 | "IsMemberFunctionPointer"); |
| 81 | static_assert(!is_member_function_pointer<int*>::value, |
| 82 | "IsMemberFunctionPointer"); |
| 83 | static_assert(!is_member_function_pointer<void*>::value, |
| 84 | "IsMemberFunctionPointer"); |
| 85 | static_assert(!is_member_function_pointer<AStruct>::value, |
| 86 | "IsMemberFunctionPointer"); |
| 87 | static_assert(!is_member_function_pointer<AStruct*>::value, |
| 88 | "IsMemberFunctionPointer"); |
| 89 | static_assert(!is_member_function_pointer<void (*)()>::value, |
| 90 | "IsMemberFunctionPointer"); |
| 91 | static_assert(!is_member_function_pointer<int (*)(int)>::value, |
| 92 | "IsMemberFunctionPointer"); |
| 93 | static_assert(!is_member_function_pointer<int (*)(int, int)>::value, |
| 94 | "IsMemberFunctionPointer"); |
aa@chromium.org | d05701e | 2013-12-06 15:55:25 +0900 | [diff] [blame] | 95 | |
avi | 486c61f | 2015-11-24 23:26:24 +0900 | [diff] [blame] | 96 | static_assert(is_member_function_pointer<void (AStruct::*)()>::value, |
| 97 | "IsMemberFunctionPointer"); |
| 98 | static_assert(is_member_function_pointer<void (AStruct::*)(int)>::value, |
| 99 | "IsMemberFunctionPointer"); |
| 100 | static_assert(is_member_function_pointer<int (AStruct::*)(int)>::value, |
| 101 | "IsMemberFunctionPointer"); |
| 102 | static_assert(is_member_function_pointer<int (AStruct::*)(int) const>::value, |
| 103 | "IsMemberFunctionPointer"); |
| 104 | static_assert(is_member_function_pointer<int (AStruct::*)(int, int)>::value, |
| 105 | "IsMemberFunctionPointer"); |
aa@chromium.org | d05701e | 2013-12-06 15:55:25 +0900 | [diff] [blame] | 106 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 107 | } // namespace |
| 108 | } // namespace base |