C ++ template: A Summary of trait Techniques

Source: Internet
Author: User

The initial purpose of trait technique is to manage template parameters. Sometimes the template requires several parameters, but some parameters are often closely related to main parameters. In this case, you can use the trait technique, export the corresponding secondary template argument from several major template parameters and appear in the template as the default template parameters. In fact, the use of trait skill instances often effectively improve program efficiency. Next I will talk about the practical application of trait in conjunction with STL.

1. _ type_traits in SGI STL

There are two types of rough description: one is to use class encapsulation, and its copy and copy operations are time-consuming. For example, deep replication is required, safe copying is required -- that is, the constructor needs to be called to complete replication; the other is the so-called pod (plain old data), which is generally a build-in type or a C-type struct type, for this type of replication and copy, you only need to copy the memory block directly, so you have the bitwise copying or trival construct/deconstruct/copy/assignment concept.

When _ type_traits is used in sgi stl, you can determine whether to copy an object during compilation to call the value assignment constructor or directly call memcpy, thus improving the efficiency. Check the source code of sgi stl:

struct __true_type {};struct __false_type {};template <class type>   //primariy templatestruct __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> { //full specialization   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;};
... // Special for other build-in Types

Next, let's take a look at how to use _ type_trairt:

Template <class inputiterator, class forwarditerator, class T> inline forwarditerator _ partition (inputiterator first, last vertex, forwarditerator result, T *) {typedef typename _ type_traits <t> :: is_pod_type is_pod; return _ uninitialized_copy_aux (first, last, result, is_pod ();} template <class inputiterator, class forwarditerator> inline forwarditerator _ uninitialized_copy_aux ( Utiterator first, inputiterator last, forwarditerator result, _ true_type) {return copy (first, last, result); // STL algorithm copy contains a large amount of information. For the pod type, call memmove, it also contains usage of generator} template <class inputiterator, class forwarditerator> forwarditerator _ generator (inputiterator first, inputiterator last, forwarditerator result, _ false_type) {forwarditerator cur = Result; for (; first! = Last; ++ first, ++ cur) construct (& * cur, * First); Return cur ;}}

Others such as uninitialized_fill and uninitialized_fill_n are similar. In addition, the csmtraits in C ++ template also demonstrates the power of trait.

2. iterator_traits in STL

Iterator_traits defines the iterator's value type, different type, reference type, pointer type, and iterator_category types. Let's just say the last one.

struct input_iterator_tag {};struct output_iterator_tag {};struct forward_iterator_tag : public input_iterator_tag {};struct bidirectional_iterator_tag : public forward_iterator_tag {};struct random_access_iterator_tag : public bidirectional_iterator_tag {};
template <class T, class Distance> struct input_iterator {  typedef input_iterator_tag iterator_category;};struct output_iterator {  typedef output_iterator_tag iterator_category;};template <class T, class Distance> struct forward_iterator {  typedef forward_iterator_tag iterator_category;};template <class T, class Distance> struct bidirectional_iterator {  typedef bidirectional_iterator_tag iterator_category;};template <class T, class Distance> struct random_access_iterator {  typedef random_access_iterator_tag iterator_category;};
// Use iterator_traits

Template <class inputiterator, class distance> inline void advance (inputiterator & I, distance N) {_ advance (I, n, Category <inputiterator >:: iterator_category ()); // construct a temporary xx_iterator_tag object} template <class inputiterator, class distance> inline void _ advance (inputiterator & I, distance N, input_iterator_tag) {While (n --) ++ I;} template <class bidirectionaliterator, class distance> inline void _ advance (bidirealialiterator & I, distance N, bidirectional_iterator_tag) {If (n> = 0) while (n --) ++ I; else while (N ++) -- I;} template <class randomaccessiterator, class distance> inline void _ advance (randomaccessiterator & I, distance N, random_access_iterator_tag) {I + = N ;}

3. char_traits in STL

Policy skills are also related to trait. trait prefers to export closely related types or constants from the master template parameters, while policy prefers behavior information, such as a function. The boundaries between trait and policy are somewhat vague. In fact, some books also have the concepts of property trait and Policy trairt. It can be seen that policy is similar to a defined algorithm (function ), there is no close logical connection with the parameters of the main template.


Reference books: STL source code analysis

C ++ Template

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.