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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Eyal Rozenberg from comment #0)
>   a.cpp:5:52: warning: comparison of unsigned expression < 0 is always false
> [-Wtype-limits]

Both branches of the condition must be instantiated by the compiler, and so
this warning (which happens in the front-end, not after optimizing the AST)
gets triggered.


> I should not be getting this warning, because when x is unsigned, the
> comparison is never performed, due to the short-circuit semantics of `and`.

Those semantics only apply at runtime.

> This can be easily determined by the compiler - and probably is.

Not in the compiler front end.

> No less
> importantly, the author of such a line in a program clearly specified
> his/her intent here with this check. 

But `x && y` doesn't prevent y being instantiated, it only prevents it being
evaluated at runtime. This is a compile-time warning based on the types
involved in the expression, and the expression is being compiled.  This is
analogous to false && func(x) which requires a definition of func(x) in the
program even if it will never get called at runtime.

If you want the comparison to be completely elided based on the type, then the
language provides a solution:

  if constexpr (is_signed_v<decltype(x)>)
    return x < 1;
  return false;

(This feature only makes sense in template code, but then so does testing
is_signed.) 

I think to avoid this warning the front end would have to disable this
particular warning for branches that depend on a compile-time constant. I don't
know if that's currently possible in GCC.

Reply via email to