STL Source Code Analysis Learning notes (ii) traits programming techniques concise routines

Source: Internet
Author: User
Tags traits

Explanatory notes

Traits Houtie Teacher's translation is extraction. The goal is to make the type recognition of template calls at compile time to do something.

The most prominent example, I think is not the "STL source Analysis" in the "iterator concept and traits programming skills" chapter of the description, but the STL algorithm in the implementation of copy . The code is in the STL source stl_algobase.h .

The final implementation of copy is broadly divided into two categories, one is the memmove operation of the whole block memory directly, and the other is the assignment of each object. Which involves the type inference of Has_trivial_assignment_operator.

If the has_trivial_assignment_operator is __true_type, a copy of the memmove operation is made, and if not, a copy of the object.

And the basic types are set in Type_traits.h with typedef __true_type Has_trivial_assignment_operator;


"Note": All types of derivation, are completed in the compilation period!!

This article temporarily ignores the discussion of T, t*, const t* type biasing (partial specilization).


Excerpt part of the code is as follows:
Type_traits.h__stl_template_null struct __type_traits<bool> {typedef __true_type HAS_TRIVIAL_DEFAULT_CONSTR   Uctor;   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_algobase.h//trivial (trivial) type, direct copy of template <class _tp> inline _tp*__copy_trivial (const _tp* __first, const _TP  * __last, _tp* __result) {memmove (__result, __first, sizeof (_TP) * (__last-__first)); return __result + (__last-__first);} Complex type, assignment template <class _inputiter, class _outputiter, class _distance> inline _outputiter __copy (_inputiter __first, _inputiter __last, _outputiter __result, Input_iterator_tag, _d  istance*) {for (; __first! = __last; ++__result, ++__first) *__result = *__first; return __result;} By judging whether Has_trivial_assignment_operator is the __true_type decision call mode template &ltClass _inputiter, Class _outputiter, class _tp> inline _outputiter __copy_aux (_inputiter __first, _inputiter __last, _outputiter __result, _tp*) {typedef typename __TYPE_TRAITS&LT;_TP&GT;::HAS_TRIVIAL_ASSIGNME  Nt_operator _trivial; Return __copy_aux2 (__first, __last, __result, _trivial ());} __false_type, use __copy copy template <class _inputiter, class _outputiter> inline _outputiter __copy_aux2 (_ Inputiter __first, _inputiter __last, _outputiter __result, __false_type) {return __copy (_ _first, __last, __result, __iterator_category (__first), __distance_type (__first));} __true_type, using __copy_trivialtemplate <class _tp> inline _tp* __copy_aux2 (_tp* __first, _Tp* __last, _Tp* __resul T, __true_type) {return __copy_trivial (__first, __last, __result);}


STL Source Code Analysis Learning notes (ii) traits programming techniques concise routines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.