http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57207
Bug #: 57207 Summary: Incorrect lifetime of const references bound to temporary Classification: Unclassified Product: gcc Version: 4.7.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: dima...@ya.ru Created attachment 30053 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30053 main.ii generated via --save-temps Here the programme: #include <iostream> struct TMyStruct { }; struct TByteArrayList { TByteArrayList(const TByteArrayList &) { i = 12; std::cout << "copy ctor " << this << std::endl; } TByteArrayList &operator=(const TByteArrayList &) { std::cout << "operator = " << this << std::endl; return *this; } TByteArrayList() { i = 12; std::cout << "ctor " << this << std::endl; } ~TByteArrayList() { std::cout << "dtor " << this << std::endl; } TByteArrayList &operator<<(const TMyStruct &) { std::cout << "<< " << this << std::endl; return *this; } int i; }; int main() { const TByteArrayList &List = TByteArrayList() << TMyStruct(); std::cout << "Here some use of List: " << List.i << " address " << &List << std::endl; return 0; } And here its output: ctor 0x7fff22f27350 << 0x7fff22f27350 dtor 0x7fff22f27350 Here some use of List: 12 address 0x7fff22f27350 Why dtor of List called right after List is created in this line const TByteArrayList &List = TByteArrayList() << TMyStruct(); Why const reference to temporary object (List) does not prolong life time of temporary object (TByteArrayList() << TMyStruct())? Seems it is a bug. //=============================================== $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)