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.