int foo and int foo()

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

    int foo and int foo()

    Hello.
    What is the standard(s) saying about the following use of the same name
    "foo" for more than one thing?

    int foo(){
    int foo;
    return foo;
    }

    int main(){
    int foo=foo();
    }

    Some older compilers are protesting (g++ 2.95.4 for example).
  • Victor Bazarov

    #2
    Re: int foo and int foo()

    "Gunnar" <gunnar@gunix.d k> wrote...[color=blue]
    > What is the standard(s) saying about the following use of the same name
    > "foo" for more than one thing?
    >
    > int foo(){
    > int foo;
    > return foo;[/color]

    This is undefined behaviour. You're using a value or an uninitialised
    integer.
    [color=blue]
    > }
    >
    > int main(){
    > int foo=foo();
    > }[/color]

    Once you initialise the 'foo' variable inside 'foo' function, the
    code becomes standard-compliant. It's OK to declare another variable
    with the same name as existing one in an enclosed scope. The inner
    variable simply hides the outer one.
    [color=blue]
    >
    > Some older compilers are protesting (g++ 2.95.4 for example).[/color]

    And what are they saying?

    Victor


    Comment

    • gabriel

      #3
      Re: int foo and int foo()

      Victor Bazarov wrote:
      [color=blue][color=green]
      >> int foo(){
      >> int foo;
      >> return foo;[/color]
      > This is undefined behaviour. You're using a value or an uninitialised
      > integer.[/color]

      Wouldn't it be undefined because the compiler would not know if the
      programmer intends to use foo the variable, or foo the function pointer?

      --
      gabriel

      Comment

      • Gunnar

        #4
        Re: int foo and int foo()

        >> int foo(){[color=blue][color=green]
        >> int foo;
        >> return foo;[/color][/color]
        [color=blue]
        > This is undefined behaviour. You're using a value or an uninitialised
        > integer.[/color]
        Yes, I know, but I need to have some adventures in life.... sorry, for
        missing that.
        [color=blue][color=green]
        >> int main(){
        >> int foo=foo();
        >> }[/color]
        >
        > Once you initialise the 'foo' variable inside 'foo' function, the
        > code becomes standard-compliant.[/color]
        Exactly what do you mean, what has the initialisation have to do with it?
        [color=blue]
        > It's OK to declare another variable
        > with the same name as existing one in an enclosed scope. The inner
        > variable simply hides the outer one.[/color]
        Ah, I see, but what about the main function.
        foo= ::foo(); seems to work.
        [color=blue][color=green]
        >> Some older compilers are protesting (g++ 2.95.4 for example).[/color]
        > And what are they saying?[/color]

        $ g++ test.cpp && ./a.out
        test.cpp: In function `int main()':
        test.cpp:13: `foo' cannot be used as a function

        Line thirteen is the line "int foo=foo();" in main.

        Comment

        • Andrey Tarasevich

          #5
          Re: int foo and int foo()

          Victor Bazarov wrote:[color=blue]
          > ...[color=green]
          >> }
          >>
          >> int main(){
          >> int foo=foo();
          >> }[/color]
          >
          > Once you initialise the 'foo' variable inside 'foo' function, the
          > code becomes standard-compliant. It's OK to declare another variable
          > with the same name as existing one in an enclosed scope. The inner
          > variable simply hides the outer one.
          > ...[/color]

          No exactly. The initializer is treated as if it _follows_ the point of
          declaration of 'int foo'. Inside the initializer identifier 'foo'
          designates a variable of type 'int'. Operator '()' cannot be applied to
          an 'int' object, which makes this code ill-formed.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          • Ron Natalie

            #6
            Re: int foo and int foo()


            "Gunnar" <gunnar@gunix.d k> wrote in message news:VVXOb.4541 2$mU6.169788@ne wsb.telia.net.. .[color=blue]
            > Hello.
            > What is the standard(s) saying about the following use of the same name
            > "foo" for more than one thing?
            >
            > int foo(){
            > int foo;
            > return foo;
            > }[/color]

            The foo in the return is the int. Other than it not being initialized it's fine.[color=blue]
            >
            > int main(){
            > int foo=foo();
            > }
            >
            > Some older compilers are protesting (g++ 2.95.4 for example).[/color]

            Newer compilers should protest here. The foo to the right of the = is the
            int on the left side. It's a syntax error to apply () to it.

            Comment

            • Victor Bazarov

              #7
              Re: int foo and int foo()

              "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote...[color=blue]
              > Victor Bazarov wrote:[color=green]
              > > ...[color=darkred]
              > >> }
              > >>
              > >> int main(){
              > >> int foo=foo();
              > >> }[/color]
              > >
              > > Once you initialise the 'foo' variable inside 'foo' function, the
              > > code becomes standard-compliant. It's OK to declare another variable
              > > with the same name as existing one in an enclosed scope. The inner
              > > variable simply hides the outer one.
              > > ...[/color]
              >
              > No exactly. The initializer is treated as if it _follows_ the point of
              > declaration of 'int foo'. Inside the initializer identifier 'foo'
              > designates a variable of type 'int'. Operator '()' cannot be applied to
              > an 'int' object, which makes this code ill-formed.[/color]

              I keep confusing those with enumerators. IIRC, declared this way

              int main() {
              enum { foo = foo() };
              }

              it should be alright. Please confirm.

              Victor


              Comment

              • Andrey Tarasevich

                #8
                Re: int foo and int foo()

                Victor Bazarov wrote:[color=blue][color=green][color=darkred]
                >> > ...
                >> >> }
                >> >>
                >> >> int main(){
                >> >> int foo=foo();
                >> >> }
                >> >
                >> > Once you initialise the 'foo' variable inside 'foo' function, the
                >> > code becomes standard-compliant. It's OK to declare another variable
                >> > with the same name as existing one in an enclosed scope. The inner
                >> > variable simply hides the outer one.
                >> > ...[/color]
                >>
                >> No exactly. The initializer is treated as if it _follows_ the point of
                >> declaration of 'int foo'. Inside the initializer identifier 'foo'
                >> designates a variable of type 'int'. Operator '()' cannot be applied to
                >> an 'int' object, which makes this code ill-formed.[/color]
                >
                > I keep confusing those with enumerators. IIRC, declared this way
                >
                > int main() {
                > enum { foo = foo() };
                > }
                >
                > it should be alright. Please confirm.
                > ...[/color]

                I assume that your question is about name resolution only. Yes, in case
                of a enumerator declaration name 'foo' in 'foo()' will be resolved to
                function 'foo'. This will trigger compiler diagnostics, since
                non-constant expression cannot be used in enumerator declaration.

                --
                Best regards,
                Andrey Tarasevich

                Comment

                • Robert Paul Clark

                  #9
                  Re: int foo and int foo()

                  > Victor Bazarov wrote:[color=blue]
                  >
                  >[color=green][color=darkred]
                  >>>int foo(){
                  >>> int foo;
                  >>> return foo;[/color]
                  >>
                  >>This is undefined behaviour. You're using a value or an uninitialised
                  >>integer.[/color]
                  >
                  >
                  > Wouldn't it be undefined because the compiler would not know if the
                  > programmer intends to use foo the variable, or foo the function pointer?
                  >[/color]
                  No. The compiler will look at the current scope and continue to look in
                  the enclosing scopes until it finds a match. In this case, the most
                  inner scope is the "int foo;" declaration, so the return would use this foo.

                  Comment

                  Working...