一个函数模板:
template<class T> T
max(T a, T b){return (a > b)? a: b;}
何时使用:
max<int>(a, b); // Yeah, the "<int>" is optional most of the time.
但是如果您允许的话,我们可以这样编写模板:
T max<class T>(T a, T b){return (a > b)? a: b;}
//I know the return type T is not in its scope, don't focus on that.
因此,我们可以像普通函数一样保持相同的声明和使用形式,甚至不需要引入和输入“template”关键字。我想类模板也是一样的吧?那么,有没有其他原因使模板成为我们今天所知道的形式呢?
我更改了形式,以便您不会关注返回类型:
auto max<class T>(T a, T b) -> T {return (a > b)? a: b;}
//This is C++11 only and ugly i guess.
//The type deduce happens at compile time
//means that return type really didn't to be a problem.