473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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).
Jul 22 '05 #1
8 2125
"Gunnar" <gu****@gunix.dk> wrote...
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
Jul 22 '05 #2
Victor Bazarov wrote:
int foo(){
int foo;
return foo;

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?

--
gabriel
Jul 22 '05 #3
>> int foo(){
int foo;
return 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.
int main(){
int foo=foo();
}


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

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.
Jul 22 '05 #4
Victor Bazarov wrote:
...
}

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


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

Jul 22 '05 #5

"Gunnar" <gu****@gunix.dk> wrote in message news:VV********************@newsb.telia.net...
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.

Jul 22 '05 #6
"Andrey Tarasevich" <an**************@hotmail.com> wrote...
Victor Bazarov wrote:
...
}

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


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
Jul 22 '05 #7
Victor Bazarov wrote:
> ...
>> }
>>
>> 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.
> ...


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


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

Jul 22 '05 #8
> Victor Bazarov wrote:

int foo(){
int foo;
return foo;


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?

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.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rick Trotter | last post by:
I was trying to write some polymorphic application code and found that a superclass method implementation gets invoked when I expect a subclass implementation. Here's how I have abstracted the...
7
by: Razvan | last post by:
Hi ! Today I saw some code like this: struct foo { foo(foo* s2){ cout << "foo::foo"; }
4
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
4
by: Alex Vinokur | last post by:
Compiler GNU gpp.exe (GCC) 3.4.1 Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that? ------ foo.cpp ------ struct Foo { explicit Foo(int) {}
9
by: Koster | last post by:
Something I've been puzzled over for a while. This really has no consequence at all, but I'm curious for the _reason_ behind where we commonly place the asterisk when declaring pointer variables....
2
by: hotadvice | last post by:
hello all Consider this: Suppose we have a an array of structure declared as following in file1.c structure foo{ ....... .......
14
by: António Marques | last post by:
Hi! I don't think I've ever been here, so I assume I'm addressing a bunch of nice people. Can you help me? I don't think there's a solution, but who knows. The thing is, picture a large...
4
by: Chaz Ginger | last post by:
I am somewhat new to Python (last year). As such I encounter little "gotchas" all the time. I am wondering is someone can explain this to me: If have three simple files: a.py...
4
by: ma740988 | last post by:
Referencing source snippet below, the actual contruction of the foo objects is done in a class. In that regard, I chose methods, class1_construct and class2_construct for demonstration purposes....
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.