Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Prototypes for functions with default arguments

Reply
Thread Tools

Prototypes for functions with default arguments

 
 
Paul Davis
Guest
Posts: n/a
 
      07-10-2003
I've just converted from gcc2.96 to gcc3.3. One of the things that 3.3
complained about was my use of functions which had default arguments.
Previously, I put the defaults in my function definition, and then
copied the definition directly as the declaration:

void A::foo(int a, int b=0); // declare somewhere
void A::foo(int a, int b=0) {...} // define in another file

3.3 doesn't like this, because the default is repeated twice. I wanted
to keep the default in the definition, because it's better documented
there, so my first attempt was to remove the defaults from the
declarations. However, this doesn't work, because the compiler may see
calls to both A:foo(x) and A::foo(x,y), and so it needs the default in
the declaration.

So, now I've got the defaults in the declarations, but not the
definitions. This compiles but I don't particularly like it. Is this
the standard way to do this? Is it actually a requirement that the
default values should only appear in one place? Stroustrup states that
"A default argument cannot be repeated or changed in a subsequent
declaration in the same scope", but I can't find anything that
prevents the default being repeated in the definition.

Thanks

Paul
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-10-2003
Paul Davis wrote in news:(E-Mail Removed):

> I've just converted from gcc2.96 to gcc3.3. One of the things that 3.3
> complained about was my use of functions which had default arguments.
> Previously, I put the defaults in my function definition, and then
> copied the definition directly as the declaration:
>
> void A::foo(int a, int b=0); // declare somewhere
> void A::foo(int a, int b=0) {...} // define in another file
>
> 3.3 doesn't like this, because the default is repeated twice. I wanted
> to keep the default in the definition, because it's better documented
> there, so my first attempt was to remove the defaults from the
> declarations. However, this doesn't work, because the compiler may see
> calls to both A:foo(x) and A::foo(x,y), and so it needs the default in
> the declaration.
>
> So, now I've got the defaults in the declarations, but not the
> definitions. This compiles but I don't particularly like it. Is this
> the standard way to do this?


Yes, Its the only way.

> Is it actually a requirement that the
> default values should only appear in one place? Stroustrup states that
> "A default argument cannot be repeated or changed in a subsequent
> declaration in the same scope", but I can't find anything that
> prevents the default being repeated in the definition.


A definition is (also) a declaration, so you're out of luck.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Paul Davis
Guest
Posts: n/a
 
      07-15-2003
On 10 Jul 2003 09:35:54 GMT, Rob Williscroft
<(E-Mail Removed)> wrote:

>Paul Davis wrote in news:(E-Mail Removed):
>
>> I've just converted from gcc2.96 to gcc3.3. One of the things that 3.3
>> complained about was my use of functions which had default arguments.
>> Previously, I put the defaults in my function definition, and then
>> copied the definition directly as the declaration:
>>
>> void A::foo(int a, int b=0); // declare somewhere
>> void A::foo(int a, int b=0) {...} // define in another file
>>
>> 3.3 doesn't like this, because the default is repeated twice. I wanted
>> to keep the default in the definition, because it's better documented
>> there, so my first attempt was to remove the defaults from the
>> declarations. However, this doesn't work, because the compiler may see
>> calls to both A:foo(x) and A::foo(x,y), and so it needs the default in
>> the declaration.
>>
>> So, now I've got the defaults in the declarations, but not the
>> definitions. This compiles but I don't particularly like it. Is this
>> the standard way to do this?

>
>Yes, Its the only way.
>
>> Is it actually a requirement that the
>> default values should only appear in one place? Stroustrup states that
>> "A default argument cannot be repeated or changed in a subsequent
>> declaration in the same scope", but I can't find anything that
>> prevents the default being repeated in the definition.

>
>A definition is (also) a declaration, so you're out of luck.
>
>HTH
>
>Rob.


Thanks Rob - I've changed everything so the defaults are now in the
declarations.

Paul
 
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
Question regarding prototypes for 0-ary functions. João Jerónimo C Programming 40 12-21-2008 10:31 PM
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM
Default arguments and prototypes Dave Vandervies C++ 5 10-03-2003 10:11 PM



Advertisments