int foo and int foo()

G

Gunnar

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).
 
V

Victor Bazarov

Gunnar said:
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;

This is undefined behaviour. You're using a value or an uninitialised
integer.
}

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.
Some older compilers are protesting (g++ 2.95.4 for example).

And what are they saying?

Victor
 
G

gabriel

Victor said:
This is undefined behaviour. You're using a value or an uninitialised
integer.

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?
 
G

Gunnar

int foo(){
This is undefined behaviour. You're using a value or an uninitialised
integer.
Yes, I know, but I need to have some adventures in life.... sorry, for
missing that.
Once you initialise the 'foo' variable inside 'foo' function, the
code becomes standard-compliant.
Exactly what do you mean, what has the initialisation have to do with it?
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.
Ah, I see, but what about the main function.
foo= ::foo(); seems to work.
And what are they saying?

$ 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.
 
A

Andrey Tarasevich

Victor said:
...

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

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.
 
R

Ron Natalie

Gunnar said:
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;
}

The foo in the return is the int. Other than it not being initialized it's fine.
int main(){
int foo=foo();
}

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

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.
 
V

Victor Bazarov

Andrey Tarasevich said:
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.

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

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

it should be alright. Please confirm.

Victor
 
A

Andrey Tarasevich

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

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

it should be alright. Please confirm.
...

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.
 
R

Robert Paul Clark

Victor said:
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?
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top