Special features of C + + templates and detailed _c language

Source: Internet
Author: User
Tags abs

Objective

When it comes to C + + templates, this is no longer a new thing, oneself in the actual development also used; for the C + + template specificity and specificity, for others, is not a new thing, but for me, it is my blind spot, that day in the group to discuss this problem, I do not have a grasp of this part, Also linked to the "STL source Analysis," a book, for this is also introduced. So, today is a detailed summary of this, in case of forgetting.

C + + templates

When it comes to C + + template specificity and specificity, we have to briefly talk about the template in C + +. We all know that strongly typed programming forces us to write pattern-consistent code for objects with the same logical structure and different data types, but not to extract the commonalities, which is not conducive to program expansion and maintenance. C + + templates have emerged. C + + templates provide definitions of common behavior for data objects that have the same logical structure. The type of the template operation object is not the actual data type, but a parameterized type. Templates in C + + are divided into class templates and function templates.

The class template is as follows:

Copy Code code as follows:

#include <iostream>
using namespace Std;

Template <class t>
Class Tclass
{
Public
member functions for Tclass

Private
T Datemember;
};

The function template is as follows:

Copy Code code as follows:

Template <class t>
T Max (const t A, const T B)
{
Return a > B? A:B;
}

Template specificity

Sometimes in order to need, for a specific type, the template needs to be specialized, that is, the so-called special treatment. For example, you have the following code:

Copy Code code as follows:

#include <iostream>
using namespace Std;

Template <class t>
Class Tclass
{
Public
BOOL Equal (const t& ARG, const t& ARG1);
};

Template <class t>
BOOL Tclass<t>::equal (const t& ARG, const t& ARG1)
{
return (arg = = arg1);
}

int main ()
{
Tclass<int> obj;
Cout<<obj. Equal (2, 2) <<endl;
Cout<<obj. Equal (2, 4) <<endl;
}

The class contains a equal method, used to compare two arguments for equality; The above code runs without any problems, but have you ever thought that in the actual development is absolutely cannot write, for the float type or double parameter, definitely cannot use "= =" the symbol to judge directly. So, for the float or double type, we need to do something special, dealing with the following:

Copy Code code as follows:

#include <iostream>
using namespace Std;

Template <class t>
Class Compare
{
Public
BOOL IsEqual (const t& ARG, const t& ARG1);
};

No longer has the meaning of template, has been clear for float
Template <>
Class compare<float>
{
Public
BOOL IsEqual (const float& ARG, const float& ARG1);
};

No longer has the meaning of template, has been explicitly double the
Template <>
Class compare<double>
{
Public
BOOL IsEqual (const double& ARG, const double& ARG1);
};

Template <class t>
BOOL Compare<t>::isequal (const t& ARG, const t& ARG1)
{
cout<< "Call compare<t>::isequal" <<endl;
return (arg = = arg1);
}

BOOL Compare<float>::isequal (const float& ARG, const float& ARG1)
{
cout<< "Call compare<float>::isequal" <<endl;
Return (ABS (ARG-ARG1) < 10e-3);
}

BOOL Compare<double>::isequal (const double& ARG, const double& ARG1)
{
cout<< "Call compare<double>::isequal" <<endl;
Return (ABS (ARG-ARG1) < 10e-6);
}

int main ()
{
Compare<int> obj;
Compare<float> obj1;
Compare<double> Obj2;
Cout<<obj. IsEqual (2, 2) <<endl;
Cout<<obj1. IsEqual (2.003, 2.002) <<endl;
Cout<<obj2. IsEqual (3.000002, 3.0000021) <<endl;
}

Template partial specificity

The above is a summary of the specificity of the template. What about the specificity of the template? The so-called template refers to the provision of another part of the definition of the formula, and it is still templatized, that is, a special version of the template parameter is designed for further constraints. The application of this kind of specificity is ubiquitous in STL. Like what:

Copy Code code as follows:

Template <class _iterator>
struct iterator_traits
{
typedef typename _iterator::iterator_category Iterator_category;
typedef typename _ITERATOR::VALUE_TYPE Value_type;
typedef typename _iterator::d ifference_type difference_type;
typedef typename _iterator::p ointer pointer;
typedef typename _iterator::reference Reference;
};

Specialize for _tp*
Template <class _tp>
struct iterator_traits<_tp*>
{
typedef random_access_iterator_tag Iterator_category;
typedef _TP VALUE_TYPE;
typedef ptrdiff_t DIFFERENCE_TYPE;
typedef _tp* Pointer;
typedef _tp& Reference;
};

Specialize for const _tp*
Template <class _tp>
struct Iterator_traits<const _tp*>
{
typedef random_access_iterator_tag Iterator_category;
typedef _TP VALUE_TYPE;
typedef ptrdiff_t DIFFERENCE_TYPE;
typedef const _TP* Pointer;
typedef const _tp& Reference;
};

Did you see that? This is the template is biased, and template specificity is the difference is that the template after the special, in fact, itself is not templatized, and the partial specificity, still with templatized. Let's look at a practical example:

Copy Code code as follows:

#include <iostream>
using namespace Std;

General design
Template <class T, class t1>
Class TestClass
{
Public
TestClass ()
{
cout<< "T, T1" <<endl;
}
};

The design of partial specificity for common pointers
Template <class T, class t1>
Class testclass<t*, t1*>
{
Public
TestClass ()
{
cout<< "t*, t1*" <<endl;
}
};

The design of the partial specificity of the const pointer
Template <class T, class t1>
Class Testclass<const t*, t1*>
{
Public
TestClass ()
{
cout<< "Const t*, t1*" <<endl;
}
};

int main ()
{
Testclass<int, char> obj;
Testclass<int *, Char *> obj1;
Testclass<const int *, char *> obj2;

return 0;
}

For the output, I will not write here, we can try.

The invocation order of the special and the partial specificity

For templates, templates, and the specificity of the template is the case, the compiler in the compilation phase of the match, how is the choice? From a philosophical point of view, we should first take care of the most special, then the second special, and finally the most common. The compiler makes the choice is also the respect from this truth. From the above example, we can also see, this is no longer illustrative.

Summarize

My understanding of the template's specificity and specificity may not be correct. I hope you and I have a discussion. I have just summed up some of my own understanding here. Finally, I also hope that you can make pertinent suggestions to my blog. I firmly believe that sharing makes us more progress.

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.