473,802 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2586
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...@versatile coding.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
2156
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
4726
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 (char *) (&a)+2 = 0x0012fc52 (char *)
9
5120
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 "extern int FOO;" in another source file (also tried "extern const int FOO;") and at link time, I get undefined references to FOO. Removed the "const" from the FOO declaration and everything worked fine.
3
2271
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
3
2357
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 in advance,
2
1898
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. Would it be more correct to make it int Foo( const char* a, const int b ) instead? I do know the effect of this would be that I can't change b in the function, which I normally never do anyway.
4
4731
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 6384690328 calls in 33.67 seconds. 33.67 6384690328 Monomial::operator(int) const Since the parameter was an int, I made it const int& since I reasoned I would be saving a copy of the int parameter to the stack by changing from a pass-by-value to...
3
6410
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 function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void foo(OutputType*) bla.cxx:10: note: void foo(PixelType*)
0
10304
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.