Default arguments and prototypes

  • Thread starter Dave Vandervies
  • Start date
D

Dave Vandervies

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
 
W

WW

Dave said:
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;
}

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

Victor Bazarov

Dave Vandervies said:
If I feed this to g++:

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;
}

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
 
W

WW

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! :)
 
K

Kevin Goodsell

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




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
 
V

Victor Bazarov

Kevin Goodsell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top