Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default function parameter values

Reply
Thread Tools

Default function parameter values

 
 
Tim Frink
Guest
Posts: n/a
 
      05-08-2008
Hi,

how can I set default values to template parameters?

I've this code:

class A
{
template<typename T>
void foo( string = "", T* = 0, bool (T::*func)(void) = 0 );
};

when I invoke A::foo with the appropriate 3 parameters, everything
works fine. However, I'd like to invoke the function foo also
with the first parameter, like objectA.foo( string("test") );
When I try to compile, I get the error message:
"no matching function for A::foo(std::string)".

Why? I set the second and third parameter of foo to default values,
so when the second and third argument are not specified, they should
be set to 0. But this seems not to work that way.

Tim
 
Reply With Quote
 
 
 
 
Tim Frink
Guest
Posts: n/a
 
      05-08-2008
> 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 );

where I do not specify the type of T either?
Where does the compiler get to information what type T is?
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-09-2008
Victor Bazarov wrote:
> ...
> Set to 0 of what type? '0' has the type 'int'. It is convertible
> to any pointer type and to any pointer-to-member type, but the
> compiler needs to know what the destination type is. It cannot
> know from the type from the expression you supplied, that's why it
> complains.
> ...


Strictly speaking, the compiler cannot deduce the type simply because
default arguments do not participate in template argument deduction at
all. The fact that integral constant '0' is convertible to many
different pointer types plays no role here.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-09-2008
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
 
Reply With Quote
 
Tim Frink
Guest
Posts: n/a
 
      05-09-2008
> You can invoke it as follows
>
> objectA.foo( string( "test" ), &objectA, &A::funcPtr );
>
> (note the extra '&').


You are completely right, I just missed the '&' in my post.


>
> 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).


Thank you, now it's clear.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter Type/Size Property Values for VARCHAR(MAX) Stored Proc Parameter jcpc ASP .Net 2 01-26-2011 11:48 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Once-only evaluation of default parameter values function definitions Fred Ma Python 8 04-13-2004 08:49 PM



Advertisments