Default arguments and prototypes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Vandervies

    Default arguments and prototypes

    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 dj3vande@csclub .uwaterloo.ca
    I disagree. The best indicator of comp.lang.c activities is an
    industrial-strength thermometer.
    --Richard Heathfield in comp.lang.c
  • WW

    #2
    Re: Default arguments and prototypes

    Dave Vandervies wrote:[color=blue]
    > 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?[/color]

    AFAIK it should not be there at all.
    [color=blue]
    > Are there any good reasons not to use this:
    > --------
    > int foo(int i=42);
    >
    > int foo(int i)
    > {
    > return i;
    > }
    > --------
    > instead?[/color]

    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


    Comment

    • Victor Bazarov

      #3
      Re: Default arguments and prototypes

      "Dave Vandervies" <dj3vande@csclu b.uwaterloo.ca> wrote...[color=blue]
      > If I feed this to g++:
      > --------
      > int foo(int i=42);
      >
      > int foo(int i=42)[/color]

      Drop the default argument value from the line above
      [color=blue]
      > {
      > 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 =[/color]
      42)'[color=blue]
      > 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.)[/color]

      According to the Standard, once a default argument value has been
      given, no other default argument is allowed to be specified.
      [color=blue]
      > Are there any good reasons not to use this:
      > --------
      > int foo(int i=42);
      >
      > int foo(int i)
      > {
      > return i;
      > }
      > --------
      > instead?[/color]

      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?
      [color=blue]
      > (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?)[/color]

      No, you're not. Every declaration in its own translation unit is
      allowed to have its own default argument value.

      Victor


      Comment

      • WW

        #4
        Re: Default arguments and prototypes

        Victor Bazarov wrote:
        [SNIPPO GROSSO][color=blue]
        > No, you're not. Every declaration in its own translation unit is
        > allowed to have its own default argument value.[/color]

        That must be fun to debug! :-)

        --
        WW aka Attila


        Comment

        • Kevin Goodsell

          #5
          Re: Default arguments and prototypes

          Victor Bazarov wrote:
          [color=blue]
          >
          > According to the Standard, once a default argument value has been
          > given, no other default argument is allowed to be specified.
          >[/color]

          <snip>
          [color=blue]
          >[color=green]
          >>(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?)[/color]
          >
          >
          > No, you're not. Every declaration in its own translation unit is
          > allowed to have its own default argument value.
          >[/color]

          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.

          Comment

          • Victor Bazarov

            #6
            Re: Default arguments and prototypes

            "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote...[color=blue]
            > Victor Bazarov wrote:
            >[color=green]
            > >
            > > According to the Standard, once a default argument value has been
            > > given, no other default argument is allowed to be specified.
            > >[/color]
            >
            > <snip>
            >[color=green]
            > >[color=darkred]
            > >>(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?)[/color]
            > >
            > >
            > > No, you're not. Every declaration in its own translation unit is
            > > allowed to have its own default argument value.
            > >[/color]
            >
            > 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?
            >[/color]

            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



            Comment

            Working...