Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default arguments and prototypes

Reply
Thread Tools

Default arguments and prototypes

 
 
Dave Vandervies
Guest
Posts: n/a
 
      10-02-2003
If I feed this to g++:
--------
int foo(int i=42);

int foo(int i=42)
{
return i;
}
--------
It says (with -W -Wall -ansi -pedantic):
--------
foo.C: In function `int foo(int = 42)':
foo.C:4: warning: default argument given for parameter 1 of `int foo(int = 42)'
foo.C:1: warning: after previous specification in `int foo(int = 42)'
--------

Does this warning indicate any actual problems, or is it just pointing
out that with the default argument given in the prototype it's not needed
in the function definition as well? (I can see how this would lead to
a minor maintenance problem with the values getting out of sync.)

Are there any good reasons not to use this:
--------
int foo(int i=42);

int foo(int i)
{
return i;
}
--------
instead?

(And, while I've got your attention, am I correct in thinking that if
only the prototype is given in another translation unit, that prototype
is required to specify the correct default value of the argument?)


dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
I disagree. The best indicator of comp.lang.c activities is an
industrial-strength thermometer.
--Richard Heathfield in comp.lang.c
 
Reply With Quote
 
 
 
 
WW
Guest
Posts: n/a
 
      10-02-2003
Dave Vandervies wrote:
> If I feed this to g++:
> --------
> int foo(int i=42);
>
> int foo(int i=42)
> {
> return i;
> }
> --------
> It says (with -W -Wall -ansi -pedantic):
> --------
> foo.C: In function `int foo(int = 42)':
> foo.C:4: warning: default argument given for parameter 1 of `int
> foo(int = 42)' foo.C:1: warning: after previous specification in `int
> foo(int = 42)' --------
>
> Does this warning indicate any actual problems, or is it just pointing
> out that with the default argument given in the prototype it's not
> needed in the function definition as well?


AFAIK it should not be there at all.

> Are there any good reasons not to use this:
> --------
> int foo(int i=42);
>
> int foo(int i)
> {
> return i;
> }
> --------
> instead?


If I am not mistaking this is the form to be used if the declaration and the
definition of the function is separate.

--
WW aka Attila


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2003
"Dave Vandervies" <(E-Mail Removed)> wrote...
> If I feed this to g++:
> --------
> int foo(int i=42);
>
> int foo(int i=42)


Drop the default argument value from the line above

> {
> return i;
> }
> --------
> It says (with -W -Wall -ansi -pedantic):
> --------
> foo.C: In function `int foo(int = 42)':
> foo.C:4: warning: default argument given for parameter 1 of `int foo(int =

42)'
> foo.C:1: warning: after previous specification in `int foo(int = 42)'
> --------
>
> Does this warning indicate any actual problems, or is it just pointing
> out that with the default argument given in the prototype it's not needed
> in the function definition as well? (I can see how this would lead to
> a minor maintenance problem with the values getting out of sync.)


According to the Standard, once a default argument value has been
given, no other default argument is allowed to be specified.

> Are there any good reasons not to use this:
> --------
> int foo(int i=42);
>
> int foo(int i)
> {
> return i;
> }
> --------
> instead?


I guess I don't understand the question. What do you mean "reasons
not to use this"? "This" is the only way the code is going to be
accepted. A good enough reason for you?

> (And, while I've got your attention, am I correct in thinking that if
> only the prototype is given in another translation unit, that prototype
> is required to specify the correct default value of the argument?)


No, you're not. Every declaration in its own translation unit is
allowed to have its own default argument value.

Victor


 
Reply With Quote
 
WW
Guest
Posts: n/a
 
      10-03-2003
Victor Bazarov wrote:
[SNIPPO GROSSO]
> No, you're not. Every declaration in its own translation unit is
> allowed to have its own default argument value.


That must be fun to debug!

--
WW aka Attila


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-03-2003
Victor Bazarov wrote:

>
> According to the Standard, once a default argument value has been
> given, no other default argument is allowed to be specified.
>


<snip>

>
>>(And, while I've got your attention, am I correct in thinking that if
>>only the prototype is given in another translation unit, that prototype
>>is required to specify the correct default value of the argument?)

>
>
> No, you're not. Every declaration in its own translation unit is
> allowed to have its own default argument value.
>


OK, I have a question along these same lines (since we're on the topic).
Is the following allowed?

void f(int, int=3);
void f(int=2, int); // add another default?

I was reading about this in the standard yesterday, and couldn't decide
whether this would be allowed. I know this is not:

void f(int, int=3);
void f(int=2, int=3); // ERROR: can't give a new default (even if
// the value is the same) in the same scope

Nor is this:

void f(int, int=3);

{
void f(int=2, int); // ERROR: Does not inherit default from
// enclosing scope, so this violates the
// rule than only trailing args have
// default values.
}

Right?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-03-2003
"Kevin Goodsell" <(E-Mail Removed)> wrote...
> Victor Bazarov wrote:
>
> >
> > According to the Standard, once a default argument value has been
> > given, no other default argument is allowed to be specified.
> >

>
> <snip>
>
> >
> >>(And, while I've got your attention, am I correct in thinking that if
> >>only the prototype is given in another translation unit, that prototype
> >>is required to specify the correct default value of the argument?)

> >
> >
> > No, you're not. Every declaration in its own translation unit is
> > allowed to have its own default argument value.
> >

>
> OK, I have a question along these same lines (since we're on the topic).
> Is the following allowed?
>
> void f(int, int=3);
> void f(int=2, int); // add another default?
>
> I was reading about this in the standard yesterday, and couldn't decide
> whether this would be allowed. I know this is not:
>
> void f(int, int=3);
> void f(int=2, int=3); // ERROR: can't give a new default (even if
> // the value is the same) in the same scope
>
> Nor is this:
>
> void f(int, int=3);
>
> {
> void f(int=2, int); // ERROR: Does not inherit default from
> // enclosing scope, so this violates the
> // rule than only trailing args have
> // default values.
> }
>
> Right?
>


Seems like it. Paragraph 4 of subclause 8.3.6 states that "In
a given function declaration, all parameters subsequent to
a parameter with a default argument shall have
default arguments supplied in this or previous declarations.".

In your example, the second parameter in the second declaration
of 'f' gets its default argument value from a previous declaration.

Comeau accepts it.

Victor



 
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
[ANN] rdp-arguments -- named arguments for 1.8 and 1.9! Roger Pack Ruby 2 01-04-2010 02:56 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 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
Prototypes for functions with default arguments Paul Davis C++ 2 07-15-2003 07:07 AM



Advertisments