473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Default function parameter values

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
Jun 27 '08 #1
6 1892
Tim Frink wrote:
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?
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"));
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.
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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
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?
Jun 27 '08 #3
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 );

where I do not specify the type of T either?
Where does the compiler get to information what type T is?
Sure. 'objectA' has a particular type. '&A::funcPtr' has it too.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
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
Jun 27 '08 #5
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
Jun 27 '08 #6
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.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
2
by: Vic Y | last post by:
Hi, I am trying to call a user defined function(UDF) from a stored proc, using a default parameter in the called UDF (e.g. @bid_price_type int = 0 ). However the calling stored proc complains...
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
21
by: Dmitry Anikin | last post by:
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload...
43
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.