Default arguments and prototypes

Discussion in 'C++' started by Dave Vandervies, Oct 2, 2003.

  1. 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
    I disagree. The best indicator of comp.lang.c activities is an
    industrial-strength thermometer.
    --Richard Heathfield in comp.lang.c
     
    Dave Vandervies, Oct 2, 2003
    #1
    1. Advertisements

  2. Dave Vandervies

    WW Guest

    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
     
    WW, Oct 2, 2003
    #2
    1. Advertisements

  3. "Dave Vandervies" <> 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, Oct 3, 2003
    #3
  4. Dave Vandervies

    WW Guest

    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
     
    WW, Oct 3, 2003
    #4
  5. 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, Oct 3, 2003
    #5
  6. "Kevin Goodsell" <> 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
     
    Victor Bazarov, Oct 3, 2003
    #6
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Davis
    Replies:
    2
    Views:
    342
    Paul Davis
    Jul 15, 2003
  2. Bo Jacobsen
    Replies:
    6
    Views:
    552
    David M. Cook
    Mar 7, 2004
  3. Edward Diener
    Replies:
    14
    Views:
    5,284
    Josiah Carlson
    Apr 6, 2004
  4. Replies:
    5
    Views:
    1,157
  5. tutmann
    Replies:
    4
    Views:
    496
Loading...

Share This Page