473,781 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Temporary King

Hi,

I would like to double check how long a temporary returned by a function
lives?

Suppose I have an instance of a class type C, which has a member function
returning some sort of wrapper-decorator by value - thereby creating an
unnamed temporary. Assuming I start using the temporary (by calling its
members) in the same expression, can I assume that it will live long enough
to serve those calls?

So assume that o.wrapper() returns a wrapper type T by value. The foo() and
bar() are member functions of that unnamed temporary of type T, where both
(at least foo) return a reference to T. The full expression is:

o.wrapper().foo ().bar();

Of course it could be continued ad nauseam. I am 99% sure that the
temporary should live long enough, but... not 101%. :-) Could someone help
me out with the right answer... and - if possible - the chapter and verse?

TIA

--
WW aka Attila
:::
We don't stop playing because we grow old, we grow old because we stop
playing.
Jul 23 '05 #1
5 1696

White Wolf wrote:
Hi,

I would like to double check how long a temporary returned by a function lives?

Suppose I have an instance of a class type C, which has a member function returning some sort of wrapper-decorator by value - thereby creating an unnamed temporary. Assuming I start using the temporary (by calling its members) in the same expression, can I assume that it will live long enough to serve those calls?

So assume that o.wrapper() returns a wrapper type T by value. The foo() and bar() are member functions of that unnamed temporary of type T, where both (at least foo) return a reference to T. The full expression is:

o.wrapper().foo ().bar();

Of course it could be continued ad nauseam. I am 99% sure that the
temporary should live long enough, but... not 101%. :-) Could someone help me out with the right answer... and - if possible - the chapter and verse?
TIA

--
WW aka Attila
:::
We don't stop playing because we grow old, we grow old because we stop playing.


Temporaries are valid until the full expression has been completed. So
your code is perfectly safe.

-shez-

Jul 23 '05 #2
shez wrote:
White Wolf wrote:

[SNIP]
o.wrapper().foo ().bar();

Of course it could be continued ad nauseam. I am 99% sure that the
temporary should live long enough, but... not 101%. :-) Could someone
help me out with the right answer... and - if possible - the chapter
and verse?


Temporaries are valid until the full expression has been completed. So
your code is perfectly safe.


That is what I know, but I was hoping for chapter and verse to support that
claim.

--
WW aka Attila
:::
'The common people like to be deceived. Deceived let them be' --Old Roman
Saying
Jul 23 '05 #3
White Wolf wrote:


So assume that o.wrapper() returns a wrapper type T by value. The foo() and
bar() are member functions of that unnamed temporary of type T, where both
(at least foo) return a reference to T. The full expression is:

o.wrapper().foo ().bar();

Of course it could be continued ad nauseam. I am 99% sure that the
temporary should live long enough, but... not 101%. :-)
100% is enough. You are right
Could someone help
me out with the right answer... and - if possible - the chapter and verse?


Sure.

12.2 Temporary objects

3 .... Temporary objcets are destroyed as the last step in evaluating the full
expression (1.9) that (lexically) contains the point where they were created.
This is true even if that evaluation ends in throwing an exception.

4 There are two contexts in which temporaries are destroyed at a different point
then the end of the fill expression. The first context is when an expression
appears as an initializer for a declarator defining an object. In that context,
the temporary that holds the result of the expression shall persist until the
object's initialization is complete. The object is initialized from a copy of
the temporary; during this copying, an implementation can call the copy constructor
many times; the temporary is destroyed after it has been copied, before or when
the initialization completes. ....

5 The second context is when a reference is bound to a temporary. The temporary to which
the reference is bound or the temporary that is the complete object to a subobject
of which the temporary is bound persists for the lifetime of the reference except
as specified below. ....
( Lots more dealing with exceptions of the above. Basically those exceptions deal
with temporaries bound to a reference in an initializer list, temporaries created
for function argument passing, return values )

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #4
Karl Heinz Buchegger wrote:
White Wolf wrote:


So assume that o.wrapper() returns a wrapper type T by value. The
foo() and bar() are member functions of that unnamed temporary of type
T, where both (at least foo) return a reference to T. The full
expression is:

o.wrapper().foo ().bar();

Of course it could be continued ad nauseam. I am 99% sure that the
temporary should live long enough, but... not 101%. :-)


100% is enough. You are right
Could someone help
me out with the right answer... and - if possible - the chapter and
verse?


Sure.

12.2 Temporary objects

3 .... Temporary objcets are destroyed as the last step in evaluating
the full expression (1.9) that (lexically) contains the point where
they were created. This is true even if that evaluation ends in
throwing an exception.

4 There are two contexts in which temporaries are destroyed at a
different point then the end of the fill expression. The first context
is when an expression appears as an initializer for a declarator
defining an object. In that context, the temporary that holds the
result of the expression shall persist until the object's
initialization is complete. The object is initialized from a copy of
the temporary; during this copying, an implementation can call the
copy constructor many times; the temporary is destroyed after it has
been copied, before or when the initialization completes. ....

5 The second context is when a reference is bound to a temporary. The
temporary to which the reference is bound or the temporary that is the
complete object to a subobject of which the temporary is bound
persists for the lifetime of the reference except as specified below.
.... ( Lots more dealing with exceptions of the above. Basically those
exceptions deal with temporaries bound to a reference in an
initializer list, temporaries created for function argument passing,
return values )


Thanks Karl! I am always afraid to look for new stuff in the standard, as I
have managed to get lost many times. But sometimes (shame on me) the answer
is simply in a section with the right title. Oh my. Thanks again.

So I guess that also means that my call-chain does count as one expression,
and no exceptional rules (or exceptions to the rules) allow that temporary
to disappear.

--
WW aka Attila
:::
If you think education is expensive, try ignorance.
Jul 23 '05 #5
"White Wolf" <wo***@freemail .hu> wrote in message
news:ct******** **@phys-news1.kolumbus. fi...
shez wrote:
White Wolf wrote: [SNIP]
o.wrapper().foo ().bar();

Of course it could be continued ad nauseam. I am 99% sure that the
temporary should live long enough, but... not 101%. :-) Could someone
help me out with the right answer... and - if possible - the chapter
and verse?


Temporaries are valid until the full expression has been completed. So
your code is perfectly safe.


That is what I know, but I was hoping for chapter and verse to support

that claim.


You asked for it! :-)

=============== =============== =============== =============== ==========
ISO/IEC 14882:1998(E)

12.2 Temporary objects

1 Temporaries of class type are created in various contexts:
binding an rvalue to a reference (8.5.3), returning an rvalue
(6.6.3), a conversion that creates an rvalue (4.1, 5.2.9,
5.2.11, 5.4), throwing an exception (15.1), entering a handler
(15.3), and in some initializations (8.5). [Note: the lifetime
of exception objects is described in 15.1. ] Even when the
creation of the temporary object is avoided (12.8), all the
semantic restrictions must be respected as if the temporary
object was created. [Example: even if the copy constructor is
not called, all the semantic restrictions, such as accessibility
(clause 11), shall be satisfied. ]

2 [Example:

class X {
// ...
public:
// ...
X(int);
X(const X&);
~X();
};

X f(X);

void g()
{
X a(1);
X b = f(X(2));
a = f(a);
}

Here, an implementation might use a temporary in which to
construct X(2) before passing it to f() using X’s copy*
constructor; alternatively, X(2) might be constructed in the
space used to hold the argument. Also, a temporary might be
used to hold the result of f(X(2)) before copying it to b
using X’s copy*constructo r; alternatively, f()’s result might
be constructed in b. On the other hand, the expression a=f(a)
requires a temporary for either the argument a or the result
of f(a) to avoid undesired aliasing of a. ]

3 When an implementation introduces a temporary object of a
class that has a non*trivial constructor (12.1), it shall
ensure that a constructor is called for the temporary object.
Similarly, the destructor shall be called for a temporary with
a non*trivial destructor (12.4). Temporary objects are destroyed
as the last step in evaluating the full*expression (1.9) that
(lexically) contains the point where they were created. This is
true even if that evaluation ends in throwing an exception.

4 There are two contexts in which temporaries are destroyed at a
different point than the end of the full*expression . The first
context is when an expression appears as an initializer for a
declarator defining an object. In that context, the temporary
that holds the result of the expression shall persist until the
object’s initialization is complete. The object is initialized
from a copy of the temporary; during this copying, an implementation
can call the copy constructor many times; the temporary is destroyed
after it has been copied, before or when the initialization completes.
If many temporaries are created by the evaluation of the initializer,
the temporaries are destroyed in reverse order of the completion of
their construction.

5 The second context is when a reference is bound to a temporary.
The temporary to which the reference is bound or the temporary
that is the complete object to a subobject of which the temporary
is bound persists for the lifetime of the reference except as
specified below. A temporary bound to a reference member in a
constructor’s ctor*initialize r (12.6.2) persists until the con-
structor exits. A temporary bound to a reference parameter in a
function call (5.2.2) persists until the completion of the full
expression containing the call. A temporary bound to the returned
value in a function return statement (6.6.3) persists until the
function exits. In all these cases, the temporaries created during
the evaluation of the expression initializing the reference, except
the temporary to which the reference is bound, are destroyed at the
end of the full*expression in which they are created and in the
reverse order of the completion of their construction. If the life-
time of two or more temporaries to which references are bound ends
at the same point, these temporaries are destroyed at that point in
the reverse order of the completion of their construction. In addition,
the destruction of temporaries bound to references shall take into
account the ordering of destruction of objects with static or automatic
storage duration (3.7.1, 3.7.2); that is, if obj1 is an object with
static or automatic storage duration created before the temporary is
created, the temporary shall be destroyed before obj1 is destroyed; if
obj2 is an object with static or automatic storage duration created
after the temporary is created, the temporary shall be destroyed after
obj2 is destroyed. [Example:

class C {
// ...
public:
C();
C(int);
friend C operator+(const C&, const C&);
~C();
};
C obj1;
const C& cr = C(16)+C(23);
C obj2;

the expression C(16)+C(23) creates three temporaries. A first
temporary T1 to hold the result of the expression C(16), a
second temporary T2 to hold the result of the expression C(23),
and a third temporary T3 to hold the result of the addition of
these two expressions. The temporary T3 is then bound to the
reference cr. It is unspecified whether T1 or T2 is created first.
On an implementation where T1 is created before T2, it is guaranteed
that T2 is destroyed before T1. The temporaries T1 and T2 are bound
to the reference parameters of operator+; these temporaries are
destroyed at the end of the full expression containing the call to
operator+. The temporary T3 bound to the reference cr is destroyed
at the end of cr’s lifetime, that is, at the end of the program.
In addition, the order in which T3 is destroyed takes into account
the destruction order of other objects with static storage duration.
That is, because obj1 is constructed before T3, and T3 is constructed
before obj2, it is guaranteed that obj2 is destroyed before T3, and
that T3 is destroyed before obj1. ]
=============== =============== =============== =============== ==========
-Mike

Jul 23 '05 #6

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

Similar topics

4
2126
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
5
4665
by: Bob Nelson | last post by:
Right next to K&R2 on my bookshelf is _C Programming: A Modern Approach_ by Professor K.N. King. The second edition of this book is now available. See this URL for details: http://knking.com/books/c2/ I don't think I'm alone among c.l.c. participants in recommending this book (based upon the very good first edition).
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
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
10076
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
9939
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
6729
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
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
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
3633
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.