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> |
| 21 | COMPILE_ASSERT(!is_pointer<int>::value, IsPointer); |
| 22 | COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer); |
| 23 | COMPILE_ASSERT(is_pointer<int*>::value, IsPointer); |
| 24 | COMPILE_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> |
| 27 | COMPILE_ASSERT(!is_array<int>::value, IsArray); |
| 28 | COMPILE_ASSERT(!is_array<int*>::value, IsArray); |
| 29 | COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray); |
| 30 | COMPILE_ASSERT(is_array<int[]>::value, IsArray); |
| 31 | COMPILE_ASSERT(is_array<const int[]>::value, IsArray); |
| 32 | COMPILE_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> |
| 35 | COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference); |
| 36 | COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference); |
| 37 | COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 38 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 39 | // is_convertible<From, To> |
ajwong@chromium.org | d5f6111 | 2011-02-17 07:37:58 +0900 | [diff] [blame] | 40 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 41 | // Extra parens needed to make preprocessor macro parsing happy. Otherwise, |
| 42 | // it sees the equivalent of: |
| 43 | // |
| 44 | // (is_convertible < Child), (Parent > ::value) |
| 45 | // |
| 46 | // Silly C++. |
| 47 | COMPILE_ASSERT( (is_convertible<Child, Parent>::value), IsConvertible); |
| 48 | COMPILE_ASSERT(!(is_convertible<Parent, Child>::value), IsConvertible); |
| 49 | COMPILE_ASSERT(!(is_convertible<Parent, AStruct>::value), IsConvertible); |
| 50 | COMPILE_ASSERT( (is_convertible<int, double>::value), IsConvertible); |
| 51 | COMPILE_ASSERT( (is_convertible<int*, void*>::value), IsConvertible); |
| 52 | COMPILE_ASSERT(!(is_convertible<void*, int*>::value), IsConvertible); |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 53 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 54 | // Array types are an easy corner case. Make sure to test that |
| 55 | // it does indeed compile. |
| 56 | COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible); |
| 57 | COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible); |
| 58 | COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 59 | |
gavinp@chromium.org | 1da28fc | 2012-06-20 03:34:08 +0900 | [diff] [blame] | 60 | // is_same<Type1, Type2> |
| 61 | COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame); |
| 62 | COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame); |
| 63 | COMPILE_ASSERT( (is_same<Parent, Parent>::value), IsSame); |
| 64 | COMPILE_ASSERT( (is_same<int*, int*>::value), IsSame); |
| 65 | COMPILE_ASSERT( (is_same<int, int>::value), IsSame); |
| 66 | COMPILE_ASSERT( (is_same<void, void>::value), IsSame); |
| 67 | COMPILE_ASSERT(!(is_same<int, double>::value), IsSame); |
ajwong@chromium.org | ec1750a | 2011-06-27 01:22:50 +0900 | [diff] [blame] | 68 | |
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> |
| 71 | COMPILE_ASSERT(is_class<AStruct>::value, IsClass); |
| 72 | COMPILE_ASSERT(is_class<AClass>::value, IsClass); |
| 73 | COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass); |
| 74 | COMPILE_ASSERT(!is_class<int>::value, IsClass); |
| 75 | COMPILE_ASSERT(!is_class<char*>::value, IsClass); |
| 76 | COMPILE_ASSERT(!is_class<int&>::value, IsClass); |
| 77 | COMPILE_ASSERT(!is_class<char[3]>::value, IsClass); |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 78 | |
aa@chromium.org | d05701e | 2013-12-06 15:55:25 +0900 | [diff] [blame] | 79 | |
| 80 | COMPILE_ASSERT(!is_member_function_pointer<int>::value, |
| 81 | IsMemberFunctionPointer); |
| 82 | COMPILE_ASSERT(!is_member_function_pointer<int*>::value, |
| 83 | IsMemberFunctionPointer); |
| 84 | COMPILE_ASSERT(!is_member_function_pointer<void*>::value, |
| 85 | IsMemberFunctionPointer); |
| 86 | COMPILE_ASSERT(!is_member_function_pointer<AStruct>::value, |
| 87 | IsMemberFunctionPointer); |
| 88 | COMPILE_ASSERT(!is_member_function_pointer<AStruct*>::value, |
| 89 | IsMemberFunctionPointer); |
| 90 | COMPILE_ASSERT(!is_member_function_pointer<int(*)(int)>::value, |
| 91 | IsMemberFunctionPointer); |
| 92 | COMPILE_ASSERT(!is_member_function_pointer<int(*)(int, int)>::value, |
| 93 | IsMemberFunctionPointer); |
| 94 | |
| 95 | COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)()>::value, |
| 96 | IsMemberFunctionPointer); |
| 97 | COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)(int)>::value, |
| 98 | IsMemberFunctionPointer); |
| 99 | COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int)>::value, |
| 100 | IsMemberFunctionPointer); |
| 101 | COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int) const>::value, |
| 102 | IsMemberFunctionPointer); |
| 103 | COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int, int)>::value, |
| 104 | IsMemberFunctionPointer); |
| 105 | COMPILE_ASSERT(is_member_function_pointer< |
| 106 | int (AStruct::*)(int, int) const>::value, |
| 107 | IsMemberFunctionPointer); |
| 108 | COMPILE_ASSERT(is_member_function_pointer< |
| 109 | int (AStruct::*)(int, int, int)>::value, |
| 110 | IsMemberFunctionPointer); |
| 111 | COMPILE_ASSERT(is_member_function_pointer< |
| 112 | int (AStruct::*)(int, int, int) const>::value, |
| 113 | IsMemberFunctionPointer); |
| 114 | COMPILE_ASSERT(is_member_function_pointer< |
| 115 | int (AStruct::*)(int, int, int, int)>::value, |
| 116 | IsMemberFunctionPointer); |
| 117 | COMPILE_ASSERT(is_member_function_pointer< |
| 118 | int (AStruct::*)(int, int, int, int) const>::value, |
| 119 | IsMemberFunctionPointer); |
| 120 | |
| 121 | // False because we don't have a specialization for 5 params yet. |
| 122 | COMPILE_ASSERT(!is_member_function_pointer< |
| 123 | int (AStruct::*)(int, int, int, int, int)>::value, |
| 124 | IsMemberFunctionPointer); |
| 125 | COMPILE_ASSERT(!is_member_function_pointer< |
| 126 | int (AStruct::*)(int, int, int, int, int) const>::value, |
| 127 | IsMemberFunctionPointer); |
| 128 | |
ajwong@chromium.org | e2cca63 | 2011-02-15 10:27:38 +0900 | [diff] [blame] | 129 | } // namespace |
| 130 | } // namespace base |