Traits programming techniques + special template + template parameter derivation + embedded type programming techniques, traitstemplate

Source: Internet
Author: User
Tags traits

[Conversion] Traits programming techniques + template skewness + template parameter derivation + embedded type programming techniques, traitstemplate

In STL, traits programming techniques have been greatly applied. Only by understanding this can we have a glimpse of the mysteries of STL.

First, record what you understand as follows:

Traits technology can be used to obtain a type of related information. First, suppose there is an iterator class with the next generic type, where the type parameter T is the type pointed to by the iterator:

Template <typename T>
Class myIterator
{
...
};

When we use myIterator, how can we know the type of the elements it points? We can add an embedded type for this class, like this:
Template <typename T>
Class myIterator
{
Typedef T value_type;
...
};
In this way, when we use the myIterator type, we can use myIterator: value_type to obtain the type pointed to by the corresponding myIterator.

Now let's design an algorithm and use this information.
Template <typename T>
Typename myIterator <T>: value_type Foo (myIterator <T> I)
{
...
}
Here we define a function Foo, which returns the type pointed to by the parameter I, that is, T. Why should we use the value_type as a mentor? That's because when we want to modify the Foo function so that it can adapt to all types of iterators, we can write as follows:
Template <typename I> // The I here can be any type of iterator
Typename I: value_type Foo (I I)
{
...
}
Now, any iterator that defines the value_type embedded type can be used as the Foo parameter, and the type of the return value of Foo will be consistent with the type of the element referred to by the corresponding iterator. So far, all the problems seem to have been solved, and we have not used any special technology. However, when the following situations are taken into account, new problems arise:

The original generated pointer can also be used as an iterator. However, we obviously cannot add a value_type embedded type to the native pointer, so that our Foo () the function cannot be applied to the original pointer. So what can we do to solve this problem? This is our main character: It's time to launch the type information extraction server Traits.

... Drum roll ......

Instead of directly using the value_type of myIterator, we can extract this information using another class:
Template <typename T>
Class Traits
{
Typedef typename T: value_type;
};
In this way, we can use Traits <myIterator>: value_type to obtain the value_type of myIterator. Therefore, we rewrite the Foo function:
Template <typename I> // The I here can be any type of iterator
Typename Traits <I >:: value_type Foo (I I)
{
...
}
However, even so, the native pointer problem remains unsolved, because the Trait class cannot obtain information about the native pointer. So we offer another tool of C ++-partial specialization ):
Template <typename T>
Class Traits <T *> // note that native pointers are specially crafted here.
{
Typedef typename T value_type;
};
Through the specific version of the Traits above, we have stated the fact that the type of elements pointed to by a pointer of the T * type is T.

In this way, our Foo function can be fully applied to native pointers. For example:
Int * p;
....
Int I = Foo (p );
Traits automatically exports the int type of the element referred to by p, so that Foo returns the correct result.

Process: embedded type-> traite class-> template special => value type that can be extracted from native pointers.

Link: http://www.cnblogs.com/kanego/archive/2012/08/15/2639761.html


Templatification

Can't the original stl code be compiled, or can you write a version by yourself?
What compiler are you using?
Some compilers do not support bitrate conversion.

In C ++, What problems does the template feature solve? That is, what is used?

By comparing the two simple examples, we can see the benefits of the template.
# Include <iostream. h>
Int abs (int x)
{Return (x> 0? X:-x );}
Long abs (long x)
{Return (x> 0? X:-x );}
Float abs (float x)
{Return (x> 0? X:-x );}
Double abs (double x)
{Return (x> 0? X:-x );}
Void main ()
{Int a =-96; long B = 78;
Float c =-3.6; double d = 5.8;
Cout <"| a | =" <abs (a) <endl;
Cout <"| B | =" <abs (B) <endl;
Cout <"| c | =" <abs (c) <endl;
Cout <"| d | =" <abs (d) <endl;
}

(Function template for absolute value)
# Include <iostream. h>
Template <typename T>
T abs (T x)
{Return (x> 0? X:-x );}
Void main ()
{Int a =-96; long B = 78;
Float c =-3.6; double d = 5.8;
Cout <"| a | =" <abs <int> (a) <endl;
Cout <"| B | =" <abs <long> (B) <endl;
Cout <"| c | =" <abs <float> (c) <endl;
Cout <"| d | =" <abs <double> (d) <endl;
}

Related Article

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.