// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TEMPLATE_UTIL_H_
#define BASE_TEMPLATE_UTIL_H_
#include <cstddef> // For size_t.
#include "build/build_config.h"
namespace base {
// template definitions from tr1
template<class T, T v>
struct integral_constant {
static const T value = v;
typedef T value_type;
typedef integral_constant<T, v> type;
};
template <class T, T v> const T integral_constant<T, v>::value;
最基础的true/false 类型, 各自内部有static的 bool成员= true/false, 其他的trait类型根据匹配结果继承相应true_type/false_type
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
如果T是一个指针的话,模板匹配会特偏化匹配到第二个,继承了true_type
template <class T> struct is_pointer : false_type {};
template <class T> struct is_pointer<T*> : true_type {};
// Member function pointer detection up to four params. Add more as needed
// below. This is built-in to C++ 11, and we can remove this when we switch.
判断某个函数是否为某个类的成员函数(最多4个参数)
template<typename T>
struct is_member_function_pointer : false_type {};
如果下面的模板匹配都没匹配到,那认为是一个非类成员函数, false_type
第一次知道原来模板可以用到这种程度,连类成员函数都可以表示出来.
0个参数
template <typename R, typename Z>
struct is_member_function_pointer<R(Z::*)()> : true_type {};
template <typename R, typename Z>
struct is_member_function_pointer<R(Z::*)() const> : true_type {};
1个参数
template <typename R, typename Z, typename A>
struct is_member_function_pointer<R(Z::*)(A)> : true_type {};
template <typename R, typename Z, typename A>
struct is_member_function_pointer<R(Z::*)(A) const> : true_type {};
2个参数
template <typename R, typename Z, typename A, typename B>
struct is_member_function_pointer<R(Z::*)(A, B)> : true_type {};
template <typename R, typename Z, typename A, typename B>
struct is_member_function_pointer<R(Z::*)(A, B) const> : true_type {};
3个参数
template <typename R, typename Z, typename A, typename B, typename C>
struct is_member_function_pointer<R(Z::*)(A, B, C)> : true_type {};
template <typename R, typename Z, typename A, typename B, typename C>
struct is_member_function_pointer<R(Z::*)(A, B, C) const> : true_type {};
4个参数
template <typename R, typename Z, typename A, typename B, typename C,
typename D>
struct is_member_function_pointer<R(Z::*)(A, B, C, D)> : true_type {};
template <typename R, typename Z, typename A, typename B, typename C,
typename D>
struct is_member_function_pointer<R(Z::*)(A, B, C, D) const> : true_type {};
判断class T 与 class U是否为同样类型,
template <class T, class U> struct is_same : public false_type {};
template <class T> struct is_same<T,T> : true_type {};
判断是否为一个数组类型
template<class> struct is_array : public false_type {};
template<class T, size_t n> struct is_array<T[n]> : public true_type {};
template<class T> struct is_array<T[]> : public true_type {};
判断是否为non const 型引用
template <class T> struct is_non_const_reference : false_type {};
template <class T> struct is_non_const_reference<T&> : true_type {};
template <class T> struct is_non_const_reference<const T&> : false_type {};
判断是否是const类型
template <class T> struct is_const : false_type {};
template <class T> struct is_const<const T> : true_type {};
判断是否是void类型
template <class T> struct is_void : false_type {};
template <> struct is_void<void> : true_type {};
namespace internal {
// Types YesType and NoType are guaranteed such that sizeof(YesType) <
// sizeof(NoType).
如同注释所说,必须保证YesType和NoType的size不同,才能被后面有效使用.
typedef char YesType;
struct NoType {
YesType dummy[2];
};
// This class is an implementation detail for is_convertible, and you
// don't need to know how it works to use is_convertible. For those
// who care: we declare two different functions, one whose argument is
// of type To and one with a variadic argument list. We give them
// return types of different size, so we can use sizeof to trick the
// compiler into telling us which function it would have chosen if we
// had called it with an argument of type From. See Alexandrescu's
// _Modern C++ Design_ for more details on this sort of trick.
这个convertHelper本质就是利用了C++本身的函数参数转化性,从实现也可以看到就是基于的不同类型适配不同的Test函数,
如果From这个类型可以在函数参数传递中转化成To,那么就是YesType, 否则NoType, 而 Create函数的作用纯粹就是为了传递
From类型。
struct ConvertHelper {
template <typename To>
static YesType Test(To);
template <typename To>
static NoType Test(...);
template <typename From>
static From& Create();
};
这个trait参考了boost库的is_class, 其基于的前提是只有是class/union/struct,都会含有一个void(C::*)(void)
类型的函数, 因此下面的Test在模板匹配时就会匹配到第一个Test函数,返回YesType, 反之则返回NoType, 注意的是
在实际使用时,调用的是Test<T>(0)
// Used to determine if a type is a struct/union/class. Inspired by Boost's
// is_class type_trait implementation.
struct IsClassHelper {
template <typename C>
static YesType Test(void(C::*)(void));
template <typename C>
static NoType Test(...);
};
} // namespace internal
// Inherits from true_type if From is convertible to To, false_type otherwise.
//
// Note that if the type is convertible, this will be a true_type REGARDLESS
// of whether or not the conversion would emit a warning.
流程如下:
首先通过ConvertHelper::Create<From>()获取了From类型,
然后ConvertHelper::Test<To>(From类型)看是否能够转化,如果可以,就返回了YesType,
从而使is_convertible这个struct之间变为integral_constant<bool,true>,后面就可以通过
is_convertible的value来拿到true/false.
template <typename From, typename To>
struct is_convertible
: integral_constant<bool,
sizeof(internal::ConvertHelper::Test<To>(
internal::ConvertHelper::Create<From>())) ==
sizeof(internal::YesType)> {
};
跟上面原理一样,通过返回的type与YesType是否size一样来得到true/false
template <typename T>
struct is_class
: integral_constant<bool,
sizeof(internal::IsClassHelper::Test<T>(0)) ==
sizeof(internal::YesType)> {
};
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
} // namespace base
#endif // BASE_TEMPLATE_UTIL_H_
chromium template_util.h 解析
最新推荐文章于 2024-01-16 19:02:44 发布