Three kinds of special features of C + + class templates

Source: Internet
Author: User
Tags abs bool comparison

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;
    }
;

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.