Traits Using C ++ templates

Source: Internet
Author: User
Tags traits

There are many articles about traits, but I feel that most of the articles are obscure, and the application description of a less complex C ++ template is too complicated. I can't help but want to share my understanding with you. Maybe I have mastered some traits, but I hope these skins will catch your eye and give you some inspiration.

First, before introducing traits, let's take a look at the C ++ templates and applications. If all you have in mind is a simple template application that implements reuse of some functions or classes, I want to tell you that you are out. Recently, I have been sorting out some template application methods. If you have time, I will write them and share them with you. This article will not discuss in detail various advanced applications of templates other than traits.
So what is traits? In fact, it is not a new concept. It was proposed in the 1990s S. It was only in this century that it was widely used in various C ++ libraries, I came into contact with this concept more than a decade later. The father of C ++, Bjarne Stroustrup, describes traits as follows:
Think of a trait as a small object whose main purpose is to carry information used by another object or algorithm to determine "policy" or "implementation details ".

I don't know how to explain traits on the official website or some books. My understanding is:
When a function, some parts of a class or encapsulated general algorithm may cause different processing or logic because of different data types (and we do not want to modify the encapsulation of the algorithm due to the difference in data types ), traits is a good solution.

I thought it would be very easy to describe it. Who knows that it is quite embarrassing to use such a long sentence. As long as you have a general concept, it is okay, even if there is no concept at all, the following will be explained through the actual code.

Let's take a look at this example. If there is a template class Test:
[Cpp]
Template <typename T>
Class Test {
......
};

Suppose there is such a requirement that some part of the Test class will be processed differently with the type T, for example, to determine whether T is a pointer type, when T is of the pointer type, the processing is different from that of the non-pointer type. How can this problem be solved?
Add a parameter to the template, as shown below?
[Cpp]
Template <typename T, bool isPointer>
Class Test {
... // Can use isPointer to judge whether T is a pointer
};

Then, the user will tell the Test class whether the current T is a pointer by transmitting one more template type. (Test <int *, true>)
Sorry, all normal users complain about such encapsulation, because users do not understand why they need to worry about whether their template type is a pointer, since it is the logic of the Test class, why bother users?
Since it is difficult to limit whether users use pointers, basic data types, or custom types when using template classes, there is no good way to judge the current T type using conventional methods. How does traits work?
Define the traits structure:
[Cpp]
Template <typename T>
Struct TraitsHelper {
Static const bool isPointer = false;
};
Template <typename T>
Struct TraitsHelper <T *> {
Static const bool isPointer = true;
};

Maybe you will be confused. There is a static constant in the struct, and there is no method or member variable. What is the purpose? The function of the first struct is to define that the default value of isPointer in all TraitsHelper is false, while that of the second struct is true when the template type T is a pointer. In other words, we can determine the current type as follows:
TraitsHelper <int >:: if the isPointer value is false, the current int type is not a pointer type.
TraitsHelper <int * >:: the isPointer value is true, and the int * of the current type is the pointer type.
Some people may think that I am talking nonsense. Please try again so that you can directly use TraitsHelper in the definition of the Test class above <T> :: isPointer to determine the current T type.
[Cpp]
If (TraitsHelper <T >:: isPointer)
......
Else
......

Let's look at the second example:
Or a template class Test:
[Cpp]
Template <typename T>
Class Test {
Public:
Int Compute (int d );
Private:
T mData;
};

It has a Compute method for some calculations. It has int-type parameters and returns int-type values.
Now the requirement has changed. When T is int type, the Compute method parameter is int and the return type is int. When T is float, the Compute method parameter is float, the return type is int. If T is of another type, the Compute method parameter is T and the return type is T. How can this problem be solved? Think about it in traits.
[Cpp]
Template <typename T>
Struct TraitsHelper {
Typedef T ret_type;
Typedef T par_type;
};
Template <>
Struct TraitsHelper <int> {
Typedef int ret_type;
Typedef int par_type;
};
Template <>
Struct TraitsHelper <float> {
Typedef float ret_type;
Typedef int par_type;
};

Then we will update the Test class as well:
[Cpp]
Template <typename T>
Class Test {
Public:
TraitsHelper <T >:: ret_type Compute (TraitsHelper <T >:: par_type d );
Private:
T mData;
};

We can see that the changes caused by different types are isolated from the Test class, and users do not need to care about these logics at all, they don't even need to know if we have used traits to solve this problem.
Here, let's get back to taste the words I said:
Traits is a good solution when some parts of functions, classes, or encapsulated general algorithms are processed or have different logic because of different data types.

Is it a bit of a feeling? If you still don't understand it, please leave a message. I will explain it again until you understand it.
Finally, let's remember it. traits, a template application, is very useful.

,

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.