const Foo& local = frobnicate ? static_cast<const Gizmo&>(Frobnicate(arg)) : arg;
Your solution is correct. Lifetime extension does propagate through static_cast
s, 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.