Detects whether the function call occurs within a constant-evaluated context. Returns true if the evaluation of the call occurs within the evaluation of an expression or conversion that is manifestly constant-evaluated; otherwise returns false.
Declared in <absl/meta/type_traits.h>
constexpr
bool
is_constant_evaluated() noexcept;
This function is implemented in terms of std::is_constant_evaluated for c++20 and up. For older c++ versions, the function is implemented in terms of __builtin_is_constant_evaluated if available, otherwise the function will fail to compile.
Applications can inspect ABSL_HAVE_CONSTANT_EVALUATED at compile time to check if this function is supported.
Example:
constexpr MyClass::MyClass(int param) { #ifdef ABSL_HAVE_CONSTANT_EVALUATED if (!absl::is_constant_evaluated()) { ABSL_LOG(INFO) << "MyClass(" << param << ")"; } #endif // ABSL_HAVE_CONSTANT_EVALUATED }
Upstream documentation:
http://en.cppreference.com/w/cpp/types/is_constant_evaluated http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#:~:text=__builtin_is_constant_evaluated
true if called within a constant-evaluated context, false otherwise.