4 ms·
Regarding the user-defined default constructor: making it constexpr makes it generate the same code as before: https://compiler-explorer.com/z/Tsbz7nd44 https:
by leni536 7mo ago
Regarding the user-defined default constructor: making it constexpr makes it generate the same code as before:
https://compiler-explorer.com/z/Tsbz7nd44 https://compiler-explorer.com/z/Tsbz7nd44
This is about constant vs dynamic initialization, not trivial vs nontrivial default construction. To be fair, the article doesn't claim this, but that's the comparison being made.
The standard allows to optimize away dynamic initialization, but AFAIK there are ABI implications of doing that, so compilers tend to not do that.
If you absolutely want to guarantee that a global is constant initialized, use "constinit" on the variable declarations too. It can also have some positive codegen effects on declarations of thread_locals.