You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Complex expressions might trigger the creation of temporaries, which might make problems even when the data memory addresses are still valid. Something like auto vf_res2 = vf1 + vf1.cross(vf2); might lead to undefined behaviour when accessing vf_res2[...] later on.
Unit tests should be written to cover the expected behaviour and guidelines should be written to explain what to avoid.
The text was updated successfully, but these errors were encountered:
Am I missing something? In your example, you take the reference to a temporary, so in the next line already, your temporary goes out of scope and is gone. How is this intended to work?
I thought that lifetime extension through binding const &s would make this work. Intuitively one might expect the resulting expression to be constructed such that it could be evaluated.
Hm, but I only know this to work locally. I don't think you can hand them around everywhere and have this work.
A temporary bound to a reference member in a constructor’s ctor-initializer (§12.6.2 [class.base.init]) persists until the constructor exits. A temporary bound to a reference parameter in a function call (§5.2.2 [expr.call]) persists until the completion of the full expression containing the call.
However, while the above quote explains why the example does not work, it does of course provide no insight, why this was defined like this and I couldn't find a satisfying answer online so far.
For a very simple example, see https://godbolt.org/z/xd7abze85
Complex expressions might trigger the creation of temporaries, which might make problems even when the data memory addresses are still valid. Something like
auto vf_res2 = vf1 + vf1.cross(vf2);
might lead to undefined behaviour when accessingvf_res2[...]
later on.Unit tests should be written to cover the expected behaviour and guidelines should be written to explain what to avoid.
The text was updated successfully, but these errors were encountered: