https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91158

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> ---
Do you know std::is_constant_evaluated (C++20)?

> Although a regular 'if' does what I want, I don't get the assurance that 'if
> contexpr' provides about no branching at runtime. Instead, I need to rely on
> the optimizer rather than on the semantics of C++.

You are using the non-standard __builtin_constant_p so you are already relying
on a compiler extension.

I don't think 'if constexpr' works as you expect. At best, your code would mean
that you have one path when evaluating a C++ constant expression, and one path
for all other cases. This could be useful if you have inline asm for speed in
the second path, or some other construction that C++ does not recognize as
valid for constexpr, and you need a simpler/slower version (first path) that
does work with constexpr, but that's it. The fact that you wrote "efficient
code" is thus suspicious.

Your use-case (with plain 'if'), if I understood it correctly, is exactly what
__builtin_constant_p is for. Whatever code you write, the compiler could always
insert a useless branch anywhere, you are already relying on the optimizer not
being (too) stupid.

I didn't check exactly why the assertion fails, so no comment yet on if it can
be changed.

Reply via email to