CGI extraction technology __type_traits

Foreword

The last article talked about the programming techniques of iterator_traits, which is a great way to make up for the shortcomings of the C++ language. STL only regulates iterators, and has developed things like iterator_traits. CGI further extended this technique to the world beyond iterators, so __type_traits was introduced. The double-dashed prefixes mean that they are all things inside the CGI STL, which are not in the STL standard specification.

The definition and function of __type_traits

In the source code __type_traits is defined in the type_traits.h file. The following is the generalized version:

 struct __true_type {

};

struct __false_type {
};

//Generalized version
template <class type>
struct __type_traits {
typedef __true_type this_dummy_member_must_be_first;

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;
};

iterator_traits is responsible for extracting the characteristics of iterators, and __type_traits is responsible for extracting type characteristics. This feature mainly refers to: has_trivial_default_constructor, has_trivial_copy_constructor, has_trivial_assignment_operator, has_trivial_destructor, is_POD_type These five types, which indicate whether there are unimportant operations such as construction, destruction, copy, and assignment, which are true and false attributes. If the answer is yes, we can take more effective measures when performing corresponding operations on this type. Instead of calling constructor and destructor, we use memory to directly process operations such as malloc(), memcpy(), etc., to achieve the highest efficiency. . For example, the properties of ordinary types such as int, float, double, char are true, and there is no need to call the type’s own construction and destruction. However, these attributes of the string class or the embedded type class are false by default, and need to call their own construction and destruction. The explanation here is different from what Hou Jie said in “STL Source Code Analysis”. Hou Jie introduced the academic name non-trivial defalt ctor, which may be more professional, but I don’t think the source code can be expressed intuitively, but the meaning is the same. If you have any questions, please leave a message for discussion.

__true_type and __false_type do not have any members, which will not bring extra burden, but they can indicate true or false. Using bool can also achieve the goal, but bool variables cannot achieve compile-time binding. CGI defines all embedded types as __false_type, which defines the most conservative value. The source code also designs specialized versions for common types, as follows:

__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;
};

__STL_TEMPLATE_NULL
struct __type_traitschar> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitschar> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<short> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitsshort> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<int> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitsint> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<long> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitslong> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<float> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<double> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<long double> {
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;
};

#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION

template
<class T>
struct __type_traits {
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;
};

#endif

__STL_TEMPLATE_NULL is a macro definition, referring to template<>. These five attributes are all __true_types of the specialized version of the common type.

__type_traits application

__type_traits has many applications in the source code. I will only cite the global function uninitialized_fill_n to illustrate. The source code is defined in the stl_uninitialized.h file, as follows:

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); //fill_n definition is given below
}

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); //analyzed above , __False_type needs to call its own structure

return cur;
}

__STL_UNWIND(destroy(first, cur));
}

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
::is_POD_type is_POD;
//Bind different functions according to the properties of is_POD span>
return __uninitialized_fill_n_aux(first, n, x, is_POD());

}

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)) ;
}

The fill_n called above is defined in the stl_glgobase.h file. The source code of the called function is as follows:

< pre>//Fill the element to the half-open and half-closed interval [first, last)

template <class ForwardIterator, class T>

void fill(ForwardIterator first, ForwardIterator last, const T& value) {

for (; first != last; ++first)

*first = value;

}

I have debugged and verified in the program. When the T type is a common type int, the function is bound to the function that calls fill_n, and when T It is a custom type, and the function is bound to a function containing construct. Verify the correctness of the above analysis process.

At this point, we found that traits are really a good thing, much better than if-else, and they are bound at compile time, which is more efficient. What a magical technique, STL source code is worth continuing to learn !

struct __true_type {

};

struct __false_type {
};

//Generalized version
template <class type>
struct __type_traits {
typedef __true_type this_dummy_member_must_be_first;

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;
};

__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;
};

__STL_TEMPLATE_NULL
struct __type_traitschar> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitschar> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<short> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitsshort> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<int> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitsint> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<long> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traitslong> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<float> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<double> {
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;
};

__STL_TEMPLATE_NULL
struct __type_traits<long double> {
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;
};

#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION

template
<class T>
struct __type_traits {
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;
};

#endif

__STL_TEMPLATE_NULL is a macro definition, referring to template<>. These five attributes are all __true_types of the specialized version of the common type.

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); //fill_n definition is given below
}

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); //analyzed above , __False_type needs to call its own structure

return cur;
}

__STL_UNWIND(destroy(first, cur));
}

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
::is_POD_type is_POD;
//Bind different functions according to the properties of is_POD span>
return __uninitialized_fill_n_aux(first, n, x, is_POD());

}

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)) ;
}

//Fill the element to the half-open and half-closed interval [first, last)

template <class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T& value) {
for (; first != last; ++first)
*first = value;
}

Leave a Comment

Your email address will not be published.