Prototypes for functions with default arguments

Discussion in 'C++' started by Paul Davis, Jul 10, 2003.

  1. Paul Davis

    Paul Davis Guest

    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
     
    Paul Davis, Jul 10, 2003
    #1
    1. Advertising

  2. Paul Davis wrote in news::

    > 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/
     
    Rob Williscroft, Jul 10, 2003
    #2
    1. Advertising

  3. Paul Davis

    Paul Davis Guest

    On 10 Jul 2003 09:35:54 GMT, Rob Williscroft
    <> wrote:

    >Paul Davis wrote in news::
    >
    >> 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
     
    Paul Davis, Jul 15, 2003
    #3
    1. Advertising

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. Dave Vandervies

    Default arguments and prototypes

    Dave Vandervies, Oct 2, 2003, in forum: C++
    Replies:
    5
    Views:
    486
    Victor Bazarov
    Oct 3, 2003
  2. Edward Diener
    Replies:
    14
    Views:
    5,117
    Josiah Carlson
    Apr 6, 2004
  3. João Jerónimo

    Question regarding prototypes for 0-ary functions.

    João Jerónimo, Nov 26, 2008, in forum: C Programming
    Replies:
    40
    Views:
    1,133
    Kaz Kylheku
    Dec 21, 2008
  4. Replies:
    1
    Views:
    593
    Keith Thompson
    May 14, 2009
  5. java
    Replies:
    0
    Views:
    138
Loading...

Share This Page