A function template:
template<class T> T
max(T a, T b){return (a > b)? a: b;}
when use it:
max<int>(a, b); // Yeah, the "<int>" is optional most of the time.
but if you allow, we can write template this way:
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.
Thus we can maintain the same form of declaration and using just like normal function does. and even don’t need to introduce and type the keyword “template”. I think class template would be the same? So is there any other reason make the template become the form we know today?
i changed the form so that you don’t focus on the return type:
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.
The immediate answer that comes to my mind is:
Just because someone who laid out the proposal for templates said so and no one on the standards committee felt that typing those extra
8characters would be an overhead.On a different note:
The syntax of templates is complicated and intimidating to begin with, making sure of presence of the keyword
templatemakes it more intuitive to a reader of code that they are dealing with templates and not any of the other beasts provided by C++ or any implementation specific constructs(read compiler extensions).