http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54293
Bug #: 54293
Summary: When a reference is bound to subobject of a temporary,
lifetime of the temporary is not extended
Classification: Unclassified
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
Google reference: b/6996555
"ISO/IEC 14882:2011(E) 12.2.5 [class.temporary]
The temporary to which the reference is bound or the temporary that is the
complete object of a subobject to which the reference is bound persists for the
lifetime of the reference except: [etc]"
With gcc-4.6, the lifetime extension is not happening at all:
FAIL int
FAIL Obj
With gcc-4.7 and 4.8 (rev 190453), lifetime is extended for Obj subobject, but
not for 'int' (or 'char', or other primitive types):
FAIL int
The test:
#include <set>
#include <iostream>
std::set<const void*> subobjs;
template <typename T>
struct ValueHolder {
explicit ValueHolder() {
subobjs.insert(&v);
}
~ValueHolder() {
subobjs.erase(&v);
}
T v;
};
struct Obj { };
bool IsValid(const void* p) {
return subobjs.find(p) != subobjs.end();
}
int main() {
const int& ref_int = ValueHolder<int>().v;
if (!IsValid(&ref_int)) {
std::cout << "FAIL int" << std::endl;
}
const Obj& ref_obj = ValueHolder<Obj>().v;
if (!IsValid(&ref_obj)) {
std::cout << "FAIL Obj" << std::endl;
}
}