C + + Template Contact Summary

Source: Internet
Author: User

Thank you for the template tutorial on the flow of chicory to the empty Ming Dynasty:

Https://github.com/wuye9036/CppTemplateTutorial

This article is to give yourself a quick reference to recall, not necessarily suitable for others to see.

#include <iostream>
usingnamespaceStd

Template <typename t>classAddfloatormulint
{
Statict do (t A, T B)
{
//In this example, the general form of what is not important, because it is not used
//just give me a 0 here.
returnT0);
}
};

//second, we want to specify that T is the code for int, which is the Special:
Template <>classaddfloatormulint<int>
{
Public:
StaticintDo (intAintb// 
{
returnA * b;
}
};

//again, we want to specify that T is the code at float time:
Template <>classaddfloatormulint<float>
{
Public:
StaticfloatDo (floatAfloatb
{
returnA + b;
}
};



intMain ()
{
intresult = addfloatormulint<int::D O (1,2);
cout<<result;

GetChar ();
return0;
}

/*
Let's take a look at the special form: It's a bit strange: template <> class addfloatormulint<int>. Don't worry, I'll explain it to you.

What is the basic form of our template?
Template <typename t> class Addfloatormulint;

But this class is for T is int, so we write
Class addfloatormulint<int>
Of course, compiling here is not a pass.

But it is not an ordinary class, but a specialization of class templates (special cases).
So I'm going to add template keyword templates to the front,
and a list of template parameters
Template < What to fill in here? > class addfloatormulint<int>;

Finally, what does the template parameter list fill in? Because the prototype T has been replaced by Int. So you can't put any extra parameters here.
So here we go.
Template <> class addfloatormulint<int>
{
// ... implementation for int ...
}

Bingo!

*/

C + + Template Contact Summary

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.