Prototypes for functions with default arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Davis

    #1

    Prototypes for functions with default arguments

    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
  • Rob Williscroft

    #2
    Re: Prototypes for functions with default arguments

    Paul Davis wrote in news:efbqgvglcv 94ig53lgtcvejgj n4rqbvgvu@4ax.c om:
    [color=blue]
    > 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?[/color]

    Yes, Its the only way.
    [color=blue]
    > 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.[/color]

    A definition is (also) a declaration, so you're out of luck.

    HTH

    Rob.
    --

    Comment

    • Paul Davis

      #3
      Re: Prototypes for functions with default arguments

      On 10 Jul 2003 09:35:54 GMT, Rob Williscroft
      <rtw@freenet.RE MOVE.co.uk> wrote:
      [color=blue]
      >Paul Davis wrote in news:efbqgvglcv 94ig53lgtcvejgj n4rqbvgvu@4ax.c om:
      >[color=green]
      >> 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?[/color]
      >
      >Yes, Its the only way.
      >[color=green]
      >> 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.[/color]
      >
      >A definition is (also) a declaration, so you're out of luck.
      >
      >HTH
      >
      >Rob.[/color]

      Thanks Rob - I've changed everything so the defaults are now in the
      declarations.

      Paul

      Comment

      Working...