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 dj******@csclub.uwaterloo.ca
I disagree. The best indicator of comp.lang.c activities is an
industrial-strength thermometer.
--Richard Heathfield in comp.lang.c 5 15376
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
"Dave Vandervies" <dj******@csclub.uwaterloo.ca> 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
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
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.
"Kevin Goodsell" <us*********************@neverbox.com> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: J.R. |
last post by:
Hi folks,
The python can only support passing value in function call (right?), I'm
wondering how to effectively pass a large parameter, such as a large list or
dictionary?
It could achieved...
|
by: Edward Diener |
last post by:
In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don't see the syntactic difference between them.
For default arguments the tutorial shows:
def...
|
by: Michele Simionato |
last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self
or other prototype-based languages. Still, from the discussion on the
mailing list, I have got the strong impression that...
|
by: Mark Hahn |
last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have
come upon the
mutable default parameter problem. For those unfamiliar with the problem,
it can be seen in this Prothon...
|
by: Paul Davis |
last post by:
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...
| |
by: Agent Mulder |
last post by:
Hi group,
I want to know why this doesn't work
void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
|
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 )
|
by: prasanthag |
last post by:
Hi,
I am a newbie to this group. I have a problem in handling the variable
arguments passed to a function. My requirement is like this.
I have 2 functions say,
void funcX(int i, int j);...
|
by: Martin Jørgensen |
last post by:
Hi,
Problem:
========
Some of my output functions are beginning to take pretty many
arguments... I mean.... We're talking about 10-15 arguments :-)
So I thought to myself, perhaps this is...
|
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,...
|
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...
| |
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |