type_traits_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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
  3. // found in the LICENSE file.
  4. #include "butil/type_traits.h"
  5. #include "butil/basictypes.h"
  6. #include <gtest/gtest.h>
  7. namespace butil {
  8. namespace {
  9. struct AStruct {};
  10. class AClass {};
  11. union AUnion {};
  12. enum AnEnum { AN_ENUM_APPLE, AN_ENUM_BANANA, AN_ENUM_CARROT };
  13. struct BStruct {
  14. int x;
  15. };
  16. class BClass {
  17. #if defined(__clang__)
  18. int ALLOW_UNUSED _x;
  19. #else
  20. int _x;
  21. #endif
  22. };
  23. class Parent {};
  24. class Child : public Parent {};
  25. // is_empty<Type>
  26. COMPILE_ASSERT(is_empty<AStruct>::value, IsEmpty);
  27. COMPILE_ASSERT(is_empty<AClass>::value, IsEmpty);
  28. COMPILE_ASSERT(!is_empty<BStruct>::value, IsEmpty);
  29. COMPILE_ASSERT(!is_empty<BClass>::value, IsEmpty);
  30. // is_pointer<Type>
  31. COMPILE_ASSERT(!is_pointer<int>::value, IsPointer);
  32. COMPILE_ASSERT(!is_pointer<int&>::value, IsPointer);
  33. COMPILE_ASSERT(is_pointer<int*>::value, IsPointer);
  34. COMPILE_ASSERT(is_pointer<const int*>::value, IsPointer);
  35. // is_array<Type>
  36. COMPILE_ASSERT(!is_array<int>::value, IsArray);
  37. COMPILE_ASSERT(!is_array<int*>::value, IsArray);
  38. COMPILE_ASSERT(!is_array<int(*)[3]>::value, IsArray);
  39. COMPILE_ASSERT(is_array<int[]>::value, IsArray);
  40. COMPILE_ASSERT(is_array<const int[]>::value, IsArray);
  41. COMPILE_ASSERT(is_array<int[3]>::value, IsArray);
  42. // is_non_const_reference<Type>
  43. COMPILE_ASSERT(!is_non_const_reference<int>::value, IsNonConstReference);
  44. COMPILE_ASSERT(!is_non_const_reference<const int&>::value, IsNonConstReference);
  45. COMPILE_ASSERT(is_non_const_reference<int&>::value, IsNonConstReference);
  46. // is_convertible<From, To>
  47. // Extra parens needed to make preprocessor macro parsing happy. Otherwise,
  48. // it sees the equivalent of:
  49. //
  50. // (is_convertible < Child), (Parent > ::value)
  51. //
  52. // Silly C++.
  53. COMPILE_ASSERT( (is_convertible<Child, Parent>::value), IsConvertible);
  54. COMPILE_ASSERT(!(is_convertible<Parent, Child>::value), IsConvertible);
  55. COMPILE_ASSERT(!(is_convertible<Parent, AStruct>::value), IsConvertible);
  56. COMPILE_ASSERT( (is_convertible<int, double>::value), IsConvertible);
  57. COMPILE_ASSERT( (is_convertible<int*, void*>::value), IsConvertible);
  58. COMPILE_ASSERT(!(is_convertible<void*, int*>::value), IsConvertible);
  59. // Array types are an easy corner case. Make sure to test that
  60. // it does indeed compile.
  61. COMPILE_ASSERT(!(is_convertible<int[10], double>::value), IsConvertible);
  62. COMPILE_ASSERT(!(is_convertible<double, int[10]>::value), IsConvertible);
  63. COMPILE_ASSERT( (is_convertible<int[10], int*>::value), IsConvertible);
  64. // is_same<Type1, Type2>
  65. COMPILE_ASSERT(!(is_same<Child, Parent>::value), IsSame);
  66. COMPILE_ASSERT(!(is_same<Parent, Child>::value), IsSame);
  67. COMPILE_ASSERT( (is_same<Parent, Parent>::value), IsSame);
  68. COMPILE_ASSERT( (is_same<int*, int*>::value), IsSame);
  69. COMPILE_ASSERT( (is_same<int, int>::value), IsSame);
  70. COMPILE_ASSERT( (is_same<void, void>::value), IsSame);
  71. COMPILE_ASSERT(!(is_same<int, double>::value), IsSame);
  72. // is_class<Type>
  73. COMPILE_ASSERT(is_class<AStruct>::value, IsClass);
  74. COMPILE_ASSERT(is_class<AClass>::value, IsClass);
  75. COMPILE_ASSERT(is_class<AUnion>::value, IsClass);
  76. COMPILE_ASSERT(!is_class<AnEnum>::value, IsClass);
  77. COMPILE_ASSERT(!is_class<int>::value, IsClass);
  78. COMPILE_ASSERT(!is_class<char*>::value, IsClass);
  79. COMPILE_ASSERT(!is_class<int&>::value, IsClass);
  80. COMPILE_ASSERT(!is_class<char[3]>::value, IsClass);
  81. // NOTE(gejun): Not work in gcc 3.4 yet.
  82. #if !defined(__GNUC__) || __GNUC__ >= 4
  83. COMPILE_ASSERT((is_enum<AnEnum>::value), IsEnum);
  84. #endif
  85. COMPILE_ASSERT(!(is_enum<AClass>::value), IsEnum);
  86. COMPILE_ASSERT(!(is_enum<AStruct>::value), IsEnum);
  87. COMPILE_ASSERT(!(is_enum<AUnion>::value), IsEnum);
  88. COMPILE_ASSERT(!is_member_function_pointer<int>::value,
  89. IsMemberFunctionPointer);
  90. COMPILE_ASSERT(!is_member_function_pointer<int*>::value,
  91. IsMemberFunctionPointer);
  92. COMPILE_ASSERT(!is_member_function_pointer<void*>::value,
  93. IsMemberFunctionPointer);
  94. COMPILE_ASSERT(!is_member_function_pointer<AStruct>::value,
  95. IsMemberFunctionPointer);
  96. COMPILE_ASSERT(!is_member_function_pointer<AStruct*>::value,
  97. IsMemberFunctionPointer);
  98. COMPILE_ASSERT(!is_member_function_pointer<int(*)(int)>::value,
  99. IsMemberFunctionPointer);
  100. COMPILE_ASSERT(!is_member_function_pointer<int(*)(int, int)>::value,
  101. IsMemberFunctionPointer);
  102. COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)()>::value,
  103. IsMemberFunctionPointer);
  104. COMPILE_ASSERT(is_member_function_pointer<void (AStruct::*)(int)>::value,
  105. IsMemberFunctionPointer);
  106. COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int)>::value,
  107. IsMemberFunctionPointer);
  108. COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int) const>::value,
  109. IsMemberFunctionPointer);
  110. COMPILE_ASSERT(is_member_function_pointer<int (AStruct::*)(int, int)>::value,
  111. IsMemberFunctionPointer);
  112. COMPILE_ASSERT(is_member_function_pointer<
  113. int (AStruct::*)(int, int) const>::value,
  114. IsMemberFunctionPointer);
  115. COMPILE_ASSERT(is_member_function_pointer<
  116. int (AStruct::*)(int, int, int)>::value,
  117. IsMemberFunctionPointer);
  118. COMPILE_ASSERT(is_member_function_pointer<
  119. int (AStruct::*)(int, int, int) const>::value,
  120. IsMemberFunctionPointer);
  121. COMPILE_ASSERT(is_member_function_pointer<
  122. int (AStruct::*)(int, int, int, int)>::value,
  123. IsMemberFunctionPointer);
  124. COMPILE_ASSERT(is_member_function_pointer<
  125. int (AStruct::*)(int, int, int, int) const>::value,
  126. IsMemberFunctionPointer);
  127. // False because we don't have a specialization for 5 params yet.
  128. COMPILE_ASSERT(!is_member_function_pointer<
  129. int (AStruct::*)(int, int, int, int, int)>::value,
  130. IsMemberFunctionPointer);
  131. COMPILE_ASSERT(!is_member_function_pointer<
  132. int (AStruct::*)(int, int, int, int, int) const>::value,
  133. IsMemberFunctionPointer);
  134. // add_const
  135. COMPILE_ASSERT((is_same<add_const<int>::type, const int>::value), AddConst);
  136. COMPILE_ASSERT((is_same<add_const<long>::type, const long>::value), AddConst);
  137. COMPILE_ASSERT((is_same<add_const<std::string>::type, const std::string>::value),
  138. AddConst);
  139. COMPILE_ASSERT((is_same<add_const<const int>::type, const int>::value),
  140. AddConst);
  141. COMPILE_ASSERT((is_same<add_const<const long>::type, const long>::value),
  142. AddConst);
  143. COMPILE_ASSERT((is_same<add_const<const std::string>::type,
  144. const std::string>::value), AddConst);
  145. // add_volatile
  146. COMPILE_ASSERT((is_same<add_volatile<int>::type, volatile int>::value),
  147. AddVolatile);
  148. COMPILE_ASSERT((is_same<add_volatile<long>::type, volatile long>::value),
  149. AddVolatile);
  150. COMPILE_ASSERT((is_same<add_volatile<std::string>::type,
  151. volatile std::string>::value), AddVolatile);
  152. COMPILE_ASSERT((is_same<add_volatile<volatile int>::type, volatile int>::value),
  153. AddVolatile);
  154. COMPILE_ASSERT((is_same<add_volatile<volatile long>::type, volatile long>::value),
  155. AddVolatile);
  156. COMPILE_ASSERT((is_same<add_volatile<volatile std::string>::type,
  157. volatile std::string>::value), AddVolatile);
  158. // add_reference
  159. COMPILE_ASSERT((is_same<add_reference<int>::type, int&>::value), AddReference);
  160. COMPILE_ASSERT((is_same<add_reference<long>::type, long&>::value), AddReference);
  161. COMPILE_ASSERT((is_same<add_reference<std::string>::type, std::string&>::value),
  162. AddReference);
  163. COMPILE_ASSERT((is_same<add_reference<int&>::type, int&>::value),
  164. AddReference);
  165. COMPILE_ASSERT((is_same<add_reference<long&>::type, long&>::value),
  166. AddReference);
  167. COMPILE_ASSERT((is_same<add_reference<std::string&>::type,
  168. std::string&>::value), AddReference);
  169. COMPILE_ASSERT((is_same<add_reference<const int&>::type, const int&>::value),
  170. AddReference);
  171. COMPILE_ASSERT((is_same<add_reference<const long&>::type, const long&>::value),
  172. AddReference);
  173. COMPILE_ASSERT((is_same<add_reference<const std::string&>::type,
  174. const std::string&>::value), AddReference);
  175. // add_cr_non_integral
  176. COMPILE_ASSERT((is_same<add_cr_non_integral<int>::type, int>::value),
  177. AddCrNonIntegral);
  178. COMPILE_ASSERT((is_same<add_cr_non_integral<long>::type, long>::value),
  179. AddCrNonIntegral);
  180. COMPILE_ASSERT((is_same<add_cr_non_integral<std::string>::type,
  181. const std::string&>::value), AddCrNonIntegral);
  182. COMPILE_ASSERT((is_same<add_cr_non_integral<const int>::type, const int&>::value),
  183. AddCrNonIntegral);
  184. COMPILE_ASSERT((is_same<add_cr_non_integral<const long>::type, const long&>::value),
  185. AddCrNonIntegral);
  186. COMPILE_ASSERT((is_same<add_cr_non_integral<const std::string>::type,
  187. const std::string&>::value), AddCrNonIntegral);
  188. COMPILE_ASSERT((is_same<add_cr_non_integral<const int&>::type, const int&>::value),
  189. AddCrNonIntegral);
  190. COMPILE_ASSERT((is_same<add_cr_non_integral<const long&>::type, const long&>::value),
  191. AddCrNonIntegral);
  192. COMPILE_ASSERT((is_same<add_cr_non_integral<const std::string&>::type,
  193. const std::string&>::value), AddCrNonIntegral);
  194. // remove_const
  195. COMPILE_ASSERT((is_same<remove_const<const int>::type, int>::value),
  196. RemoveConst);
  197. COMPILE_ASSERT((is_same<remove_const<const long>::type, long>::value),
  198. RemoveConst);
  199. COMPILE_ASSERT((is_same<remove_const<const std::string>::type,
  200. std::string>::value), RemoveConst);
  201. COMPILE_ASSERT((is_same<remove_const<int>::type, int>::value), RemoveConst);
  202. COMPILE_ASSERT((is_same<remove_const<long>::type, long>::value), RemoveConst);
  203. COMPILE_ASSERT((is_same<remove_const<std::string>::type, std::string>::value),
  204. RemoveConst);
  205. // remove_reference
  206. COMPILE_ASSERT((is_same<remove_reference<int&>::type, int>::value),
  207. RemoveReference);
  208. COMPILE_ASSERT((is_same<remove_reference<long&>::type, long>::value),
  209. RemoveReference);
  210. COMPILE_ASSERT((is_same<remove_reference<std::string&>::type,
  211. std::string>::value), RemoveReference);
  212. COMPILE_ASSERT((is_same<remove_reference<const int&>::type, const int>::value),
  213. RemoveReference);
  214. COMPILE_ASSERT((is_same<remove_reference<const long&>::type, const long>::value),
  215. RemoveReference);
  216. COMPILE_ASSERT((is_same<remove_reference<const std::string&>::type,
  217. const std::string>::value), RemoveReference);
  218. COMPILE_ASSERT((is_same<remove_reference<int>::type, int>::value), RemoveReference);
  219. COMPILE_ASSERT((is_same<remove_reference<long>::type, long>::value), RemoveReference);
  220. COMPILE_ASSERT((is_same<remove_reference<std::string>::type, std::string>::value),
  221. RemoveReference);
  222. } // namespace
  223. } // namespace butil