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

            Bug ID: 104486
           Summary: if constexpr versus -Wtype-limits
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: drepper.fsp+rhbz at gmail dot com
  Target Milestone: ---

One use of 'if constexpr' is to handle different type sizes.  This is a simple
example:

#include <climits>

using some_type = int;

some_type f();

bool foo()
{
  if constexpr (sizeof(some_type) == sizeof(int))
    return f() == INT_MAX;
  else if constexpr (sizeof(some_type) == sizeof(long))
    return f() == LONG_MAX;
  else
    return f() == LONG_LONG_MAX;
}


Compiling this code with -Wtype-limits produces warnings:

g++ -std=gnu++20 -c u.cc -Wtype-limits
u.cc: In function ‘bool foo()’:
u.cc:12:16: warning: comparison is always false due to limited range of data
type [-Wtype-limits]
   12 |     return f() == LONG_MAX;
      |                ^
u.cc:14:16: warning: comparison is always false due to limited range of data
type [-Wtype-limits]
   14 |     return f() == LONG_LONG_MAX;
      |                ^

These statements are guarded by 'if constexpr' which causes the code in the
respective branch to not be active.  The warnings should be disabled for those
code blocks.  Again, the whole point of writing code like this is to avoid
overflow errors and the like.

Reply via email to