473,407 Members | 2,315 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,407 software developers and data experts.

int& foo() {} works well?

hello,
anyone else can explain why following codes give wrong result while
compiler accept it still?

int & foo() {}
int main()
{
int x = foo;
std::cout << x << std::endl;
}

thanks!
Oct 17 '08 #1
3 2561
bingfeng wrote:
hello,
anyone else can explain why following codes give wrong result while
compiler accept it still?

int & foo() {}
int main()
{
int x = foo;
std::cout << x << std::endl;
}
After including the missing headers, I still get an error:

invalid conversion from 'int& (*)()' to 'int'

So changing

int x = foo;

to

int x = foo();

I get a clean compile but undefined behavior, e.g., as per [6.6.3/2].
As for _why_ the standard specifies undefined behavior for falling through
the end of a function that has to return something, one has to recall that
it is generally undecidable which paths of a function can be taken for
possible input values. In the most common cases, it is easy but still a
burden on the compiler that the C++ standard does not wish to impose.
Best

Kai-Uwe Bux
Oct 17 '08 #2
On 2008-10-17 06:17:59 -0400, Kai-Uwe Bux <jk********@gmx.netsaid:
bingfeng wrote:
>hello,
anyone else can explain why following codes give wrong result while
compiler accept it still?

int & foo() {}
int main()
{
int x = foo;
std::cout << x << std::endl;
}

After including the missing headers, I still get an error:

invalid conversion from 'int& (*)()' to 'int'

So changing

int x = foo;

to

int x = foo();

I get a clean compile but undefined behavior, e.g., as per [6.6.3/2].
As for _why_ the standard specifies undefined behavior for falling through
the end of a function that has to return something, one has to recall that
it is generally undecidable which paths of a function can be taken for
possible input values. In the most common cases, it is easy but still a
burden on the compiler that the C++ standard does not wish to impose.
In addition, most compilers will give a warning on something as simple
as the code above. So they'll find the obvious problems, but not the
subtle ones.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 17 '08 #3
Thanks, Pete and Kai, I was misled to check how initialiate a
reference in standard text found nothing of course. Common used g++
(3.4.5) and MSVC compiler keep silence on this both.
Sorry for my typo in original post, Kai.

On 10ÔÂ17ÈÕ, ÏÂÎç7ʱ18·Ö, Pete Becker <p...@versatilecoding.comwrote:
On 2008-10-17 06:17:59 -0400, Kai-Uwe Bux <jkherci...@gmx.netsaid:
bingfeng wrote:
hello,
anyone else can explain why following codes give wrong result while
compiler accept it still?
int & foo() {}
int main()
{
int x = foo;
std::cout << x << std::endl;
}
After including the missing headers, I still get an error:
invalid conversion from 'int& (*)()' to 'int'
So changing
int x = foo;
to
int x = foo();
I get a clean compile but undefined behavior, e.g., as per [6.6.3/2].
As for _why_ the standard specifies undefined behavior for falling through
the end of a function that has to return something, one has to recall that
it is generally undecidable which paths of a function can be taken for
possible input values. In the most common cases, it is easy but still a
burden on the compiler that the C++ standard does not wish to impose.

In addition, most compilers will give a warning on something as simple
as the code above. So they'll find the obvious problems, but not the
subtle ones.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Oct 17 '08 #4

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

Similar topics

8
by: Gunnar | last post by:
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(){
8
by: aling | last post by:
Given following code snippets: int a; int* p=(int*)((&a)+1); when I debuged the code using IDE like VC7.1, I found that: a = 0x0012fc50 (int ) p = 0x0012fc78 (int *) (&a)+1 = 0x0012fc51...
9
by: C. J. Clegg | last post by:
When you say "const int FOO = 0" (as the commonly-recommended C++ alternative to "#define FOO 0"), isn't that declaration globally visible? I have "const int FOO = 0;" in one source file and...
3
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
3
by: Michael | last post by:
Hi, I am a little confused about & and int &. Do they both mean reference? int a; int* b= & a ; (& here mean reference of a) int& b = a ; ( How to understand & precisely here?) Thanks...
2
by: Jim Langston | last post by:
In my function/method paramater list I try to keep constant correctness. I.E. I would use: int Foo( const char* a, int b ) instead of int Foo( char* a, int b ) My question reguards int b. ...
4
by: 9lives.9lives | last post by:
Hello, everyone! I am trying to optimize some code, but I don't think I'm doing what I think I'm doing. I profiled my code and found that the overloaded operator of my monomial class did...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.