https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97435
Bug ID: 97435 Summary: Lifetime of temporaries not correctly extending when optiimzation are enabled Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: chris at chrissavoie dot com Target Milestone: --- Godbolt example: https://godbolt.org/z/fssed1 The following code: struct A { __attribute__((noinline)) A(int i) : data(i) {} A& operator+=(int i) { data += i; return *this; } int data; }; int main() { A const& b = A(5) += 5; A c(6); return b.data + c.data; // 16 expected } Will return 16 when compiled without optimizations, 11 when compiled with optimizations (-O1 or greater) and 6 with noinline disabled and optimizations on. Browsing versions on godbolt it looks like the inline version of the bug appeared between versions 4.9.3 and 5.1. The noinline version appeared between versions 4.6.4 and 4.7.3.