1 // Copyright (c) 2011 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 #ifndef BASE_TEMPLATE_UTIL_H_
6 #define BASE_TEMPLATE_UTIL_H_
9 #include <cstddef> // For size_t.
11 #include "build/build_config.h"
15 // template definitions from tr1
17 template<class T
, T v
>
18 struct integral_constant
{
19 static const T value
= v
;
21 typedef integral_constant
<T
, v
> type
;
24 template <class T
, T v
> const T integral_constant
<T
, v
>::value
;
26 typedef integral_constant
<bool, true> true_type
;
27 typedef integral_constant
<bool, false> false_type
;
29 template <class T
> struct is_pointer
: false_type
{};
30 template <class T
> struct is_pointer
<T
*> : true_type
{};
32 template <class T
, class U
> struct is_same
: public false_type
{};
33 template <class T
> struct is_same
<T
,T
> : true_type
{};
35 template<class> struct is_array
: public false_type
{};
36 template<class T
, size_t n
> struct is_array
<T
[n
]> : public true_type
{};
37 template<class T
> struct is_array
<T
[]> : public true_type
{};
39 template <class T
> struct is_non_const_reference
: false_type
{};
40 template <class T
> struct is_non_const_reference
<T
&> : true_type
{};
41 template <class T
> struct is_non_const_reference
<const T
&> : false_type
{};
43 template <class T
> struct is_void
: false_type
{};
44 template <> struct is_void
<void> : true_type
{};
48 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
56 // This class is an implementation detail for is_convertible, and you
57 // don't need to know how it works to use is_convertible. For those
58 // who care: we declare two different functions, one whose argument is
59 // of type To and one with a variadic argument list. We give them
60 // return types of different size, so we can use sizeof to trick the
61 // compiler into telling us which function it would have chosen if we
62 // had called it with an argument of type From. See Alexandrescu's
63 // _Modern C++ Design_ for more details on this sort of trick.
65 struct ConvertHelper
{
66 template <typename To
>
67 static YesType
Test(To
);
69 template <typename To
>
70 static NoType
Test(...);
72 template <typename From
>
76 // Used to determine if a type is a struct/union/class. Inspired by Boost's
77 // is_class type_trait implementation.
78 struct IsClassHelper
{
80 static YesType
Test(void(C::*)(void));
83 static NoType
Test(...);
86 } // namespace internal
88 // Inherits from true_type if From is convertible to To, false_type otherwise.
90 // Note that if the type is convertible, this will be a true_type REGARDLESS
91 // of whether or not the conversion would emit a warning.
92 template <typename From
, typename To
>
94 : integral_constant
<bool,
95 sizeof(internal::ConvertHelper::Test
<To
>(
96 internal::ConvertHelper::Create
<From
>())) ==
97 sizeof(internal::YesType
)> {
100 template <typename T
>
102 : integral_constant
<bool,
103 sizeof(internal::IsClassHelper::Test
<T
>(0)) ==
104 sizeof(internal::YesType
)> {
109 #endif // BASE_TEMPLATE_UTIL_H_