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