struct
__true_type {
};
struct
__false_type {
};
template
<
class
type>
struct
__type_traits {
...
typedef
__false_type has_trivial_default_constructor;
typedef
__false_type has_trivial_copy_constructor;
typedef
__false_type has_trivial_assignment_operator;
typedef
__false_type has_trivial_destructor;
typedef
__false_type is_POD_type;
};
默认情况下,认为所有类型都为non-trivial,下面再给出内置类型的特化版本,如:
__STL_TEMPLATE_NULL
struct
__type_traits<
char
> {
typedef
__true_type has_trivial_default_constructor;
typedef
__true_type has_trivial_copy_constructor;
typedef
__true_type has_trivial_assignment_operator;
typedef
__true_type has_trivial_destructor;
typedef
__true_type is_POD_type;
};
这里,就认为char类型具有trivial的构造和复制控制。注意,这些嵌套类型要么返回
__true_type类型,要么返回__false_type类型,根据类型不同而编译时期确定该调用哪个版本的函数。template
<
class
ForwardIterator,
class
Size,
class
T>
inline
ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n,
const
T& x) {
return
__uninitialized_fill_n(first, n, x, value_type(first));
}
template
<
class
Iterator>
inline
typename
iterator_traits<Iterator>::value_type*
value_type(
const
Iterator&) {
// 决定迭代器value_type
return
static_cast
<
typename
iterator_traits<Iterator>::value_type*>(0);
}
template
<
class
ForwardIterator,
class
Size,
class
T,
class
T1>
inline
ForwardIterator __uninitialized_fill_n(ForwardIterator first, Size n,
const
T& x, T1*) {
typedef
typename
__type_traits<T1>::is_POD_type is_POD;
return
__uninitialized_fill_n_aux(first, n, x, is_POD());
}
template
<
class
ForwardIterator,
class
Size,
class
T>
inline
ForwardIterator
__uninitialized_fill_n_aux(ForwardIterator first, Size n,
const
T& x, __true_type) {
return
fill_n(first, n, x);
}
template
<
class
ForwardIterator,
class
Size,
class
T>
ForwardIterator
__uninitialized_fill_n_aux(ForwardIterator first, Size n,
const
T& x, __false_type) {
ForwardIterator cur = first;
__STL_TRY {
for
( ; n > 0; --n, ++cur)
construct(&*cur, x);
return
cur;
}
__STL_UNWIND(destroy(first, cur));
}
“类型萃取器”__type_traits,布布扣,bubuko.com
原文:http://blog.csdn.net/nestler/article/details/24575857