https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97553
Jakub Jelinek <jakub at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Whether the function is constexpr or not doesn't really matter when you evaluate it in non-constant expression contexts. In those the ubsan instrumentation is bypassed (the constant expression evaluation does similar checking), but otherwise it is a normal function like any other, which including the instrumentation is inlined etc. And, the runtime sanitization intentionally isn't heavily optimized away, because the intent is to detect when the code is invalid, so it can't e.g. optimize away those checks based on assumption that undefined behavior will not happen. If you want a constant via C++ means, use int foo() { constexpr int x = g().length(); return x; }