>I tried it, and you're right! I guess that 'int' is what it assumes as a
>default typename if not given one... Cool to know! : )
Not exactly. The template type parameters are deduced from the function
arguments. You only need to specify them yourself using the f<type>()
syntax if the template parameter isn't represented in the function argument
list, or it is, and it can't be deduced for some reason, such as calling
f(x, y), where both parameters have the type T, but x is (say) an int and y
is a long and thus aren't the same type. To fix that, you can say f<int>(x,
y).

Signature
Doug Harrison
Visual C++ MVP
Peter Oliphant - 20 Oct 2005 21:11 GMT
Interesting, and thanks for the explanation! : )
But then, how do you explain that the following works:
int result = Add_T( 1, 2 ) ;
Here the parameters are not 'typed', since '1' and '2' could be 'long' or
'int' or 'unsigned' etc. Although I'm guessing that, as parameters,
constants of the form of an integer without further casting will be assumed
to be an 'int'. Or it could be grabbing the parameter type from the 'result'
type perhaps (since it is structured to be the same type in as out)?
[==P==]
>>I tried it, and you're right! I guess that 'int' is what it assumes as a
>>default typename if not given one... Cool to know! : )
[quoted text clipped - 9 lines]
> f<int>(x,
> y).
Doug Harrison [MVP] - 20 Oct 2005 22:18 GMT
>Interesting, and thanks for the explanation! : )
>
[quoted text clipped - 7 lines]
>to be an 'int'. Or it could be grabbing the parameter type from the 'result'
>type perhaps (since it is structured to be the same type in as out)?
But 1 and 2 are integer literals that have the type int. For more on this,
see:
C++ Integer Constants
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pl
uslang_C.2b2b_.Integer_Constants.asp?frame=true
There are even more obscure rules that aren't covered there, that determine
the type when the constant won't fit in its "preferred" type, and this
further depends on the form of the constant, e.g. decimal vs. hex, and
whether or not there's a suffix. Here's a page that covers that:
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ib
m.xlcpp8a.doc/language/ref/conref.htm

Signature
Doug Harrison
Visual C++ MVP