Tim Frink wrote:
>> Because the compiler needs to know what 'T' is. It cannot deduce
>> the type from an integral expression ('0'). You need to tell it
>> what the type T is by supplying the template argument, like
>>
>> objectA.foo<sometype>(string("test"));
>
> But why can I invoke the function with:
>
> objectA.foo( string( "test" ), objectA, &A::funcPtr );
You cannot. The second argument must have a pointer type. Your 'objectA'
is not a pointer (and user-defined conversion operators, if any, will
not be considered in this case). Therefore, the above invocation is
ill-formed.
You can invoke it as follows
objectA.foo( string( "test" ), &objectA, &A::funcPtr );
(note the extra '&').
> where I do not specify the type of T either?
> Where does the compiler get to information what type T is?
It is called "function template argument deduction". When you specify an
explicit argument for the corresponding parameter, the compiler will
analyze the type of the argument you supplied, and _deduce_ the meaning
of 'T' from that type. For example, in the above (corrected) call, the
meaning of 'T' can be deduced from either the second or the third
argument. It has to be 'A'.
When you don't supply explicit arguments, the complier has nothing to
deduce the meaning of 'T' from. (Default arguments are ignored by the
deduction process).
--
Best regards,
Andrey Tarasevich


|