473,396 Members | 1,998 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,396 software developers and data experts.

Hiding const references bound to temporary

Hi All,

Given the following code :

char const & identity( char const &c ) { return c; }

char const & f( ) { return identity('A'); }
char const & g( ) { return 'A'; }

Of course, the compiler complains of function 'g' returning a
reference to a temporary. But the compiler (I used gcc 4.2.x) doesn't
say anything regarding the function 'f'. I know that temporaries bound
to constant references have a life span that extends to the expression
in which the function call where the bounding took place is evaluated.
Now given that, is the fact of returning in turn the constant
reference returned by 'identity' considered to be part of the
expression in which 'identity' is evaluated, ie the expression is

return identity('A');

or does the evaluated expression just consist of the function call
'identity', ie:

identity('A');

?

I would have said the first case. But I'm not sure. I guess the main
question is: does a call to 'f' yield undefined behaviour ?

Thanks for your comments,

Olivier.
Oct 14 '08 #1
2 2344
casul wrote:
Given the following code :

char const & identity( char const &c ) { return c; }

char const & f( ) { return identity('A'); }
char const & g( ) { return 'A'; }

Of course, the compiler complains of function 'g' returning a
reference to a temporary. But the compiler (I used gcc 4.2.x) doesn't
say anything regarding the function 'f'. I know that temporaries bound
to constant references have a life span that extends to the expression
in which the function call where the bounding took place is evaluated.
Now given that, is the fact of returning in turn the constant
reference returned by 'identity' considered to be part of the
expression in which 'identity' is evaluated, ie the expression is

return identity('A');
No, it's not an expression. It's a *control statement*. As part of the
statement the expression 'identity('A')' is evaluated (and it's a full
expression).
or does the evaluated expression just consist of the function call
'identity', ie:

identity('A');

?
There is no semicolon in an expression. A semicolon is a *statement*
separator. Drop the semicolon and you got yourself an expression.
I would have said the first case. But I'm not sure. I guess the main
question is: does a call to 'f' yield undefined behaviour ?
IMHO, yes. The temporary disappears as soon as the full expression is
evaluated. By the time anybody attempts to use the return value of the
'identity' function call with 'A' as the argument in any way, the
reference returned has become invalid. Any use of it (like passing up
to the caller of 'f') would be undefined behaviour.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 14 '08 #2
On Oct 14, 8:35*pm, casul <olivier.gr...@gmail.comwrote:
Given the following code :
* *char const & identity( char const &c ) { return c; }
* *char const & f( ) { return identity('A'); }
* *char const & g( ) { return 'A'; }
Of course, the compiler complains of function 'g' returning a
reference to a temporary. But the compiler (I used gcc 4.2.x)
doesn't say anything regarding the function 'f'.
How could it? Suppose that identity() and f() where in
different translation units. How could the compiler know that f
is in fact returning a reference to it's temporary?
I know that temporaries bound to constant references have a
life span that extends to the expression in which the function
call where the bounding took place is evaluated.
Then you know something that is completely wrong. A temporary
has a lifetime until the end of the full expression in which it
appears. *IF* the temporary is used to initialize a reference,
it's lifetime is extended to match that of the reference (except
that a temporary bound to a reference being returned explicitly
doesn't have its lifetime extended). But whether the temporary
is bound to some other reference or not has no effect on its
lifetime.
Now given that, is the fact of returning in turn the constant
reference returned by 'identity' considered to be part of the
expression in which 'identity' is evaluated, ie the expression
is
* * return identity('A');
or does the evaluated expression just consist of the function
call 'identity', ie:
* * identity('A');
?
The temporary 'A' is used to initialize the reference which is
an argument of identity. The lifetime of that reference ends on
return from identity, which is *before* the end of the full
expression. So the lifetime of the temporary is at the end of
the full expression. (Actually, there's a slight extension
here; if the temporary were used to directly initialize the
return value, it's lifetime would be extended until the return
value was constructed. That's not the case here, however.)
I would have said the first case. But I'm not sure. I guess
the main question is: does a call to 'f' yield undefined
behaviour ?
Of course.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 14 '08 #3

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

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
5
by: Dave | last post by:
Hello all, I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that object were modified, what harm could...
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
3
by: Rick Helmus | last post by:
Hello all In a few classes I have overloaded functions for C style strings and STL strings like this class SomeClass { void f(const char *s); void f(const std::string &s); };
3
by: rwf_20 | last post by:
Hi, I'm looking at the differences between: const NonTrivialObject& obj = functionThatReturnsANonTrivialObjectByValue(); and: const NonTrivialObject obj =
3
by: George2 | last post by:
Hello everyone, I am debugging MSDN code from, http://msdn2.microsoft.com/en-us/library/0eestyah(VS.80).aspx here is my output, 1>main.cpp
5
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to...
4
by: Giovanni Gherdovich | last post by:
Hello, I'm doing some toy experiments to see how the algoritm std::transform and the function adapter std::bind2nd can play together, but my compiler give my the error error: passing `const...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.