概念:
Metadata
:能够在编译期被操作的东西就是元数据,分成两类:类型和非类型,这两类都可以做为模板参数。整数类型,函数的指针或引用,成员指针(这些都可以转化成整数,因此是合法的)
Metafunction
:类模板,它的所有参数都是类型;
类,有公共的可访问的内嵌类; 多态:允许使用不同的参数类型的性质就是多态,字面上即“多种形式的能力”; 在C ++中,多态有两种:静态多态,即编译期多态;动态多态,即运行期多态; 编译期多态使得我们可以在编译期做出选择,而这正是我们所向往追求的。
Boost 的type traits library :这是MPL 的基石 以下代码抽自boost/ 或自己的替代实现,知其然,知其所以然,我们后面使用到的时候才可以了然于胸 1. primary type is_xxx 系列的metafunction 具有成员value, 而且我们只关心成员value 的值为true/false
template
<
bool
x
>
struct
bool_
...
{
static bool const value = x; typedef bool_ < x > type; typedef bool value_type; operator bool () const ... { return x; } };
typedef bool_
<
false
>
false_; typedef bool_
<
true
>
true_;
is_xxx 系列派生自bool_, 下面逐一给出is_xxx 的实现is_integral 通过对integral 类型进行特化达到
template
<
typename type
>
struct
is_integral : false_
...
{}
;
//
即默认所有类型均不是integral类型
//
at this moment, variadic template still has nothing to do with this condition
template
<
bool
arg0,
bool
arg1,
bool
arg2
=
false
,
bool
arg3
=
false
,
bool
arg4
=
false
,
bool
arg5
=
false
,
bool
arg6
=
false
,
bool
arg7
=
false
,
bool
arg8
=
false
,
bool
arg9
=
false
>
struct
ice_or
...
{
static const bool value = arg0 + arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9; }
; template
<
bool
arg0,
bool
arg1,
bool
arg2
=
true
,
bool
arg3
=
true
,
bool
arg4
=
true
,
bool
arg5
=
true
,
bool
arg6
=
true
,
bool
arg7
=
true
,
bool
arg8
=
true
,
bool
arg9
=
true
>
struct
ice_and
...
{
static const bool value = arg0 * arg1 * arg2 * arg3 * arg4 * arg5 * arg6 * arg7 * arg8 * arg9; }
;
// 然后对可枚举的integral 类型进行特化,其中还有对应的const/volatile/const volatile 版本
template
<>
struct
is_integral
<
char
>
: true_...
...
{}
; template
<>
struct
is_integral
<
const
char
>
: true_...
...
{}
; template
<>
struct
is_integral
<
volatile
char
>
: true_...
...
{}
; template
<>
struct
is_integral
<
const
volatile
char
>
: true_...
...
{}
;
C ++中的integral 类型有以下:bool / char / unsigned char / signed char / wchar_t / short / unsigned short / int / unsigned int / long / unsigned long / long long / unsigned long long 分别对以上类型进行特化,我们就可以得到完整的is_integral is_float 的实现跟is_integral 相同,c++ 中,float 类型只有float/double/long double ,还有对应的const/volatile/const volatile版本;
template
<
typename type
>
struct
is_void : false_
...
{}
; template
<>
struct
is_void
<
void
>
: true_
...
{}
; template
<>
struct
is_void
<
const
void
>
: true_
...
{}
; template
<>
struct
is_void
<
const
volatile
void
>
: true_
...
{}
;
Is_function 使用variadic template 实现,否则需要根据函数的参数个数一一进行特化,很麻烦,c++0x 直接支持variadic template , conceptgcc 已经实现了variadic template 功能
template
<
typename type
>
struct
is_function : false_
...
{}
; template
<
typename retType, typename ... argType
>
struct
is_function
<
retType (
*
)(argType...)
>
: true_
...
{}
;
template
<
typename type
>
struct
is_member_function : false_
...
{}
; template
<
typename retType, typename classType, typename ... argType
>
struct
is_member_function
<
retType (classType::
*
)(argType...)
>
: true_
...
{}
;
template
<
typename type
>
struct
is_array : false_...
...
{}
; template
<
typename type,
int
N
>
struct
is_array
<
type[N]
>
: true_...
...
{}
; template
<
typename type
>
struct
is_array
<
type[]
>
: true_...
...
{}
;
//
然后是对应的const/volatile/const volatile版本true_{};
i
s_member_pointer 的实现类似于
is_function ,其中,
is_member_pointer 分为两种:
1 ,类成员数据指针;
2 ,类成员函数指针
template
<
typename type
>
struct
is_member_pointer :false_
...
{}
; template
<
typename dataType, typename classType
>
struct
is_member_pointer
<
dataType classType::
*>
: true_
...
{}
; template
<
typename retType, typename classType, … argType
>
struct
is_member_pointer
<
retype (classType::
*
)(argType…)
>
: true_
...
{}
;
//
还有对应的const/volatile/const volatile版本
template
<
typename type
>
struct
is_pointer : false_
...
{}
; template
<
typename type
>
struct
is_ponter
<
type
*>
: true_
...
{}
; template
<
typename type
>
struct
is_pointer : is_function
<
type
>
...
{}
; template
<
typename type
>
struct
is_pointer : is_member_pointer
<
type
>
...
{}
;
template
<
typename type
>
struct
is_reference : false_
...
{}
; template
<
typename type
>
struct
is_reference
<
type
&>
: true_
...
{}
;
//
还有对应的const/volatile/const volatile版本
C ++中的类类型包括:class/struct/union 定义的类型,对于类类型,可以定义其类成员指针,包括成员数据指针及成员函数指针,boost就是利用这个性质来区分是否为类类型
template
<
typename type
>
char
class_helper(
int
type::
*
); template
<
typename type
>
int
class_helper(...); template
<
typename type
>
struct
is_class_helper
...
{
static const bool value = sizeof (class_helper < type > ( 0 )) == sizeof ( char ) ? true : false ; }
; template
<
typename type
>
struct
is_class : bool_
<
is_class_helper
<
type
>
::value
>
...
{}
;
现在,剩下is_union/is_enum 未实现,这几个比较麻烦,需要编译器提供相当的支持,boost 提供保守的支持,就是将is_union/is_enum 都默认为false_ 。
2. secondary type
template
<
typename type
>
struct
is_arithmetic : boos_
...
<ice_or<is_integral<type>::value, is_float<type>::value>::value>{}
;
is_fundamental 的实现:包含算术类型和void 类型
template
<
typename type
>
struct
is_fundamental: bool_
...
<ice_or<is_arithmetic<type>::value, is_void<type>::value>::value>{}
;
根据C ++标准,复合类型包括数组/ 函数/ 指针/ 引用/ 类类型/ 枚举/ 成员指针,或者说,除了基础类型,其它的都是复合类型,而基础类型则只包括算术类型和void 类型。
template
<
typename type
>
struct
is_compound: bool_
<!
is_fundamental
<
type
>
::value
>
...
{}
;
在C ++中,对象是指除了函数和引用之外的所有东西
template
<
typename type
>
struct
is_object: bool_
<!ice_or<is_function<type>::value, is_reference<type>::value>::value>{};