I have this code where the I call member functions on a temporary object of Test class
not production code: https://godbolt.org/z/8YW7EqEf6
const auto &t2 = Test().withT1().withT2();
testParam(t2);
With the above function calls to withT1
and withT2
the temporary object gets destroyed immediately. And, a destoryed object (t2
) is sent to the function testParam
However, if I do not call the member functions withT1
and withT2
the temp object, it is assigned correctly to reference variable t2
, and holds it lifetime until end on block scope.
not production code: https://godbolt.org/z/9hEanEeGY
Can someone please help me understand why the compiler behaves this way?
1