473,320 Members | 2,006 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.

Is returning a reference to a constant undefined?

Gentlemen:
Is this UB?
#include <iostream>

const int& f()
{
return 3;
}

int main()
{
const int& s = f();
std::cout << s << '\n';
}
My compiler does warn me about it, but what I would like to know is
this:

Wouldn't the "const int&" part bind to the temporary like in the case
of:

const int& i = 3;

?

Thanks for your help.
Old Admiral salutes you.
OA.

Jul 23 '05 #1
3 1092
Old Admiral wrote:
Gentlemen:
Is this UB?
#include <iostream>

const int& f()
{
return 3;
}

int main()
{
const int& s = f();
std::cout << s << '\n';
}
It is undefined behaviour because you *return* a reference to a local
object.
My compiler does warn me about it, but what I would like to know is
this:

Wouldn't the "const int&" part bind to the temporary like in the case
of:

const int& i = 3;

No they are not the same. But by only changing the function signature to:
const int f()
we can say that this is the same and valid.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2
Old Admiral wrote:
Gentlemen:
Is this UB?
Yes, you're returning a reference to a local literal.
#include <iostream>

const int& f()
{
return 3;
}

int main()
{
const int& s = f();
std::cout << s << '\n';
}
My compiler does warn me about it, but what I would like to know is
this:

Wouldn't the "const int&" part bind to the temporary like in the case
of:

const int& i = 3;

?


No. In the case of

const int & i = 3;

both 3 and 'i' have the same scope. In your case, 3 disappears as soon
as the function where it exists finishes.

V
Jul 23 '05 #3
Bazarov, Vranos:

Thanks for your help.
OA

Jul 23 '05 #4

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

Similar topics

2
by: nielson | last post by:
I have a class which contains (1) a class variable and (2) a method (e.g., methodA) which returns a reference to the class variable. If the variable has been assigned a value by a constant...
15
by: Andy C | last post by:
I am new to python, so please bear with me if I am making some conceptual error. Basically I want to create a graph with an adjacency list representation, but I don't want any of the adjacency...
4
by: Kees Hoogendijk | last post by:
Hi everyone, Actually I dont know how te explein this, that's why I paste my code below. I've a main en I've a few header files. After I include the headers, the compiler shows the errors as...
19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
3
by: bp | last post by:
Hi, Could someone to tell me what I'm doing wrong? I'm using gcc version 3.3.4 and ld version 2.15.90.0.3 20040415 the linker message is: .../obj/classOpenAreaForm.o(.text+0x211): In...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: Naren | last post by:
Hi , Is it correct to return reference of a member variable. because it can be like we have already deleted the object and still holding the reference of member variable. A *a = new A; int...
17
by: djcredo | last post by:
Hey all, I want to return a pointer to a struct. Here is what I'lm trying to do: struct Position{ int x; int y; }; Position* GraphicTag::getRenderCentre(){
3
by: Kermit Mei | last post by:
Hello all, I wrote four simple c++ source files for this test. Look please: /*************1***************/ /// file A.h #ifndef A_H #define A_H class A { public: void set(int i);
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.