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?