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

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> ---
namespace std {
  constexpr inline bool
  is_constant_evaluated () noexcept
  {
    return __builtin_is_constant_evaluated ();
  }
}

int
foo ()
{
  if (std::is_constant_evaluated ())
    return 1;
  return 0;
}

constexpr int
bar ()
{
  if (std::is_constant_evaluated ())
    return 1;
  if constexpr (std::is_constant_evaluated ())
    return 2;
  return 0;
}

consteval int
baz ()
{
  if (std::is_constant_evaluated ())
    return 1;
  return 0;
}

In foo, we know it will always evaluate to false, in baz always to true.
In non-constexpr functions it will not evaluate everywhere to false, so one
would need to leave out e.g. initializers of static variables and the other
spots which are manifestly constant evaluated.
But say if conditions except for statement expressions in them and if it is not
the init might be always ok and good enough for the warning.

Reply via email to