The C++ Core Guidelines recommend using lambda initialization for complex initialization: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-lambda-init
It gives the following reason:
Reason It nicely encapsulates local initialization, including cleaning up scratch variables needed only for the initialization, without needing to create a needless nonlocal yet nonreusable function. It also works for variables that should be const but only after some initialization work.
These examples use GCC 9.1 on x86-64
using -O2
.