C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pointer to function with default argument value

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Tony
Guest





PostPosted: Fri Jan 16, 2004 1:02 pm    Post subject: pointer to function with default argument value Reply with quote



C++ Gurus,

What does the standard say about the following case ?


bool func1(double d1, double d2=1.0)
{
return d1 < d2;
}

bool func2(bool (*pf)(double d1, double d2=2.0))
{
return pf(1.0);
}

int main()
{
bool (*pf)(double d1, double d2=3.0);

pf = func1;
pf(1.0); //d2 = ? in func1 when func1 is called?
//in MSC++6.0, d2=3.0, is this standard?
func2(pf); //d2 = ? in func1 when func2 calls func1 ?
//in MSC++6.0, d2=2.0, is this standard?

return 0;

/*About declaration of "bool (*pf)(double d1, double d2=3.0);"
pf must have the above form or "bool (*pf)(double, double=3.0);".
If the declaration is "bool (*pf)(double d1, double d2);",
"pf(1.0);" has compiler error, even if func1 has d2=0.0
as default. Is this behaviour standard? */
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel Frey
Guest





PostPosted: Sat Jan 17, 2004 1:31 am    Post subject: Re: pointer to function with default argument value Reply with quote



Tony wrote:
Quote:
What does the standard say about the following case ?

bool func2(bool (*pf)(double d1, double d2=2.0))

Illegal, as a default argument may not appear in a function pointer
declaration. See 8.3.6/3.

Regards, Daniel

--
Daniel Frey

aixigo AG - financial solutions & technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: [email]daniel.frey (AT) aixigo (DOT) de[/email], web: http://www.aixigo.de


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Sat Jan 17, 2004 3:09 am    Post subject: Re: pointer to function with default argument value Reply with quote



Tony wrote:
Quote:
C++ Gurus,

What does the standard say about the following case ?

Section 8.3.6, paragraph 3 says:

"A default argument expression shall be specified only in the
parameter-declaration-clause of a function declaration or in a
template-parameter (14.1). If it is specified in a parameter-
declaration-clause, it shall not occur within a declarator or
abstract-declarator of a parameter-declaration. 8Cool"

Footnote 88 says:

"This means that default arguments cannot appear, for example,
in declarations of pointers to functions, references to
functions, or typedef declarations."

So these declarations are ill-formed:

<snip>
Quote:
bool func2(bool (*pf)(double d1, double d2=2.0))
snip
bool (*pf)(double d1, double d2=3.0);
snip


If your compiler permits them, that's an extension and the
standard has nothing to say about their behaviour.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Sat Jan 17, 2004 3:21 am    Post subject: Re: pointer to function with default argument value Reply with quote

On Fri, 16 Jan 2004 08:02:42 -0500, Tony wrote:

Quote:
C++ Gurus,

What does the standard say about the following case ?

bool func1(double d1, double d2=1.0)
{ return d1 < d2; }

bool func2(bool (*pf)(double d1, double d2=2.0))
{ return pf(1.0); }

You may have default parameters only in the function definition or
declaration. The second example should be illegal.


Regards,
-Dhruv.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tony
Guest





PostPosted: Tue Jan 20, 2004 11:09 am    Post subject: Re: pointer to function with default argument value Reply with quote

bool func1(double d1, double d2=2.0)
{
return d1 < d2;
}

bool (*pf)(double, double); //this is only the legal form
pf = func1;
pf(1.0, 2.0);
pf(1.0); //this has compiler error in MSC++6.0.
//how to call func1 with d2 as default via pf?
//impossible in the standard ??

Tony


"Dhruv"
Quote:
On Fri, 16 Jan 2004 08:02:42 -0500, Tony wrote:

C++ Gurus,

What does the standard say about the following case ?

bool func1(double d1, double d2=1.0)
{ return d1 < d2; }

bool func2(bool (*pf)(double d1, double d2=2.0))
{ return pf(1.0); }

You may have default parameters only in the function definition or
declaration. The second example should be illegal.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.