[PATCH] libstdc++: Define std::__is_constant_evaluated() for internal use

Jakub Jelinek jakub@redhat.com
Fri Nov 26 12:39:50 GMT 2021


On Fri, Nov 26, 2021 at 12:29:25PM +0000, Jonathan Wakely via Gcc-patches wrote:
> +  // Internal version of std::is_constant_evaluated() for C++11.
> +  // This can be used without checking if the compiler supports the built-in.
> +  constexpr inline bool
> +  __is_constant_evaluated() noexcept
> +  {

When you have such a nice one spot, shouldn't it:
#if __cpp_if_consteval >= 202106L 
  if consteval
    {
      return true;
    }
  else
    {
      return false;
    }
#elif __has_builtin(__builtin_is_constant_evaluated)
...

Theoretically not all compilers need to support the builtin and in C++23
mode if consteval should be slightly more efficient.

One disadvantage of std::__is_constant_evaluated() is that Marek's
warning for if constexpr (std::is_constant_evaluated()) will not trigger
if __is_constant_evaluated() is used instead.  But I'd hope testsuite
coverage would discover it quickly if such bug would appear...

	Jakub



More information about the Gcc-patches mailing list