Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1287

Answer by HolyBlackCat for Ternary operator and prolonging the lifetime of a temporary object via reference-to-const

$
0
0
const Foo& local = frobnicate ? static_cast<const Gizmo&>(Frobnicate(arg)) : arg;

Your solution is correct. Lifetime extension does propagate through static_casts, among other things, so this will not dangle.

The dangling observed in the other answer appears to be an old compiler bug, it doesn't happen in modern Clang, GCC, and MSVC.

Cppreference says it was always the case, I don't see any "since C++XX" or related defect reports in the link above.


Viewing all articles
Browse latest Browse all 1287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>