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.
5 #include "base/template_util.h"
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
18 class Child
: public Parent
{};
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
);
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
);
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
);
39 // is_convertible<From, To>
41 // Extra parens needed to make preprocessor macro parsing happy. Otherwise,
42 // it sees the equivalent of:
44 // (is_convertible < Child), (Parent > ::value)
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
);
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
);
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
);
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
);