Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is returning a reference to a constant undefined?

Reply
Thread Tools

Is returning a reference to a constant undefined?

 
 
Old Admiral
Guest
Posts: n/a
 
      03-14-2005
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.

 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      03-14-2005
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2005
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
 
Reply With Quote
 
Old Admiral
Guest
Posts: n/a
 
      03-15-2005
Bazarov, Vranos:

Thanks for your help.


OA

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
returning constant reference of element from a template cuneyt C++ 2 07-15-2005 06:31 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM
Returning a reference to an existing C++ object as a reference JustMe Perl Misc 1 08-29-2003 07:02 AM



Advertisments