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

lifetime extension of temoraries

http://www.artima.com/cppsource/foreach.html says

"temporary objects of derived type can have their lifetime extended by
binding them to a const reference to the base type."

====================================
#include <iostream>

struct base {};

struct derived : public base {
~derived() { std::cout << "derived is dead\n"; }
};

struct ref {
const base& ref_;
ref(const base& x) : ref_(x) {}
};

int main() {
ref r = derived();
std::cout << "ref is alive\n";
}
====================================

If this is the case, how come the output of the above code (in VC++8
and G++ 3.4.4) is

derived is dead
ref is alive
Curiously, if "ref r" is changed to "const base& r", the output
changes for both compilers, AND is now also compiler-dependent (two
temporaries in VC++)

Judging by this thread http://groups.google.com/group/comp....7a1fd87bf1b85e
there seems to be little consensus on how the standard should be
interpreted regarding lifetime extensions?

Mar 22 '07 #1
1 1736
n.************@gmail.com wrote:
http://www.artima.com/cppsource/foreach.html says

"temporary objects of derived type can have their lifetime extended by
binding them to a const reference to the base type."

====================================
#include <iostream>

struct base {};

struct derived : public base {
~derived() { std::cout << "derived is dead\n"; }
};

struct ref {
const base& ref_;
ref(const base& x) : ref_(x) {}
};

int main() {
ref r = derived();
std::cout << "ref is alive\n";
}
====================================

If this is the case, how come the output of the above code (in VC++8
and G++ 3.4.4) is

derived is dead
ref is alive
And what part of the standard says is in question? You are binding the
temporary to a ref object not a const reference to the base type. Once
the base copy operation is done, the derived is dead.
The compiler generates a temporary, which is bound to a const base &,
which is then used to copy construct a ref object.

Fei
>
Curiously, if "ref r" is changed to "const base& r", the output
changes for both compilers, AND is now also compiler-dependent (two
temporaries in VC++)

Judging by this thread http://groups.google.com/group/comp....7a1fd87bf1b85e
there seems to be little consensus on how the standard should be
interpreted regarding lifetime extensions?
Mar 22 '07 #2

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

Similar topics

13
by: John Harrison | last post by:
Rather a long program I'm afraid but I don't think I can cut it down any further. What I'm trying to do is construct a complex object Y from several X objects in a complex expression. I'm trying...
6
by: Jason Heyes | last post by:
I am interested in the lifetime of a function argument in two cases. They are: 1. void foo(Bar bar); 2. void foo(const Bar &bar); In each case I call foo like so: foo(Bar());
8
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
0
by: JD | last post by:
I'm currently writing a class to manage per-user configuration files, using the same format as the app.config file. This will be used in a Winforms app to manage user options, window positions,...
14
by: Frederick Gotham | last post by:
There is a common misconception, (one which I myself also held at one point), that a const reference can "extend the lifetime of a temporary". Examples such as the following are given: Snippet...
3
by: nagashre | last post by:
class A { public: A():a(0), b(0){} handleMyMsg( char* aa, char*bb); private: processMessage();
3
by: mario semo | last post by:
Hello, What does the C++ Norm says about the lifetime of compiler generated temporary variables? #include <stdio.h> class BaseRef {...
6
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; }...
5
by: Juha Nieminen | last post by:
Let's assume we have a class like this: //--------------------------------------------------------- #include <iostream> class MyClass { public: MyClass() { std::cout << "constructor\n"; }...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.