Speaking of C + + template and template specificity, I believe many people are familiar with, but when it comes to the template of several types of specificity, I believe that people are not a lot of understanding. Here I have summed up several types of template specificity, one is to become an absolute type, but rather as a reference, pointer type, and three is special to another template class.
Here is a simple example to illustrate these three situations:
General version
Template<class t>
class Compare
{public
:
static bool IsEqual (const T & LH, const t& RH)
{return
LH = RH;
}
};
This is a template class for comparison, which can have a variety of functions for comparison, taking IsEqual as an example.
One, special to absolute type
In other words, it is the most common way for us to make special specific types, such as the special float,double
specialize for float
template<>
class compare<float>
{public
:
static bool IsEqual (const float& LH, const float& RH)
{return
abs (LH-RH) < 10e-3;
}
};
specialize for double
template<>
class compare<double>
{public
:
static BOOL IsEqual (const double& LH, const double& RH)
{return
abs (LH-RH) < 10e-6;
}
;