C + + templates template and template __c++

Source: Internet
Author: User
Tags define function

Original link: https://blog.csdn.net/skyleung/article/details/42195509

Template<class t> and Template<typename t> can be used to define function templates and class templates, and they are not fundamentally different in use.

In earlier versions of C + +, the keyword was not typename, so class was used when the template was defined.

In later versions of C + +, in order to no longer be confused with class, a new keyword TypeName is added to differentiate. For me personally, the use of the TypeName keyword is more advocated when defining template functions.


Here, by the way, document the definition and use of template functions and template classes. The user defines only the template function before compiling the program

Template <typename T, TypeName t2>

void func (T1 t1, T2 T2) {}

and Template classes

Template <typename T, TypeName t2>

Class Mytemplateclass

{

。。。

};


And a call to somewhere else:

Func (5, "Hello");

Func ("World", 5.60);

Mytemplateclass<int, string> *instance = Newmytemplateclass<int, string> ();

Mytemplateclass<string, string> Instance2 ();

When is the specific function or class generated? At compile time, after the compilation completes, all the concrete functions and the concrete classes are generated.

At compile time, the compiler generates func (int, char*) and func (char*, float) based on the type of the Func () argument. The generation of template classes is also the same, generating new classes based on instance and Instance2, and compiling them.


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.