std::is_constant_evaluated(3)
π std::is_constant_evaluated(3) π
okay so bestie, welcome to the std::is_constant_evaluated(3) page from the C++ Standard Libary manual. this one is all about asking the compiler βhey, are we in a constant-evaluated context rn?β and getting a very cute true/false back. periodt. π
β¨ NAME β¨
std::is_constant_evaluated β literally std::is_constant_evaluated, no cap. thatβs the name. π«Ά
π¦ Synopsis π¦
Defined in header <type_traits>
constexpr bool is_constant_evaluated() noexcept; (since C++20)
This function detects whether the function call occurs within a constant-evaluated context. It returns true if the evaluation of the call occurs within the evaluation of an expression or conversion that is manifestly constant-evaluated; otherwise it returns false.
To determine whether initializers of following variables are manifestly constant-evaluated, compilers may first perform a trial constant evaluation:
- variables with reference type or
const-qualified integral or enumeration type; staticand thread local variables.
It is not recommended to depend on the result in this case. fr fr, donβt build your whole vibe on this edge case.
int y = 0;
const int a = std::is_constant_evaluated() ? y : 1;
// Trial constant evaluation fails. The constant evaluation is discarded.
// Variable a is dynamically initialized with 1
const int b = std::is_constant_evaluated() ? 2 : y;
// Constant evaluation with std::is_constant_evaluated() == true succeeds.
// Variable b is statically initialized with 2
π Parameters π
(none)β yes bestie, zero parameters. iconic. π
πΈ Return value πΈ
true if the evaluation of the call occurs within the evaluation of an expression or conversion that is manifestly constant-evaluated; otherwise false. Thatβs it. Thatβs the whole vibe. β¨
π§ Possible implementation π§
// This implementation requires C++23 if consteval.
constexpr bool is_constant_evaluated() noexcept
{
if consteval
{
return true;
}
else
{
return false;
}
}
π Notes π
When directly used as the condition of static_assert declaration or constexpr if statement, std::is_constant_evaluated() always returns true. important! no cap.
Because if consteval is absent in C++20, std::is_constant_evaluated is typically implemented using a compiler extension.
Feature-test macro info, slay:
- Feature-test macro:
__cpp_lib_is_constant_evaluated - Value:
201811L - Std:
(C++20) - Feature:
std::is_constant_evaluated
π¦ Example π¦
// Run this code
#include <cmath>
#include <iostream>
#include <type_traits>
constexpr double power(double b, int x)
{
if (std::is_constant_evaluated() && !(b == 0.0 && x < 0))
{
// A constant-evaluation context: Use a constexpr-friendly algorithm.
if (x == 0)
return 1.0;
double r {1.0};
double p {x > 0 ? b : 1.0 / b};
for (auto u = unsigned(x > 0 ? x : -x); u != 0; u /= 2)
{
if (u & 1)
r *= p;
p *= p;
}
return r;
}
else
{
// Let the code generator figure it out.
return std::pow(b, double(x));
}
}
int main()
{
// A constant-expression context
constexpr double kilo = power(10.0, 3);
int n = 3;
// Not a constant expression, because n cannot be converted to an rvalue
// in a constant-expression context
// Equivalent to std::pow(10.0, double(n))
double mucho = power(10.0, n);
std::cout << kilo << " " << mucho << "\n"; // (3)
}
πΊ Output πΊ
1000 1000
π See also π
constexprspecifier(C++11) β specifies that the value of a variable or function can be computed at compile time.constevalspecifier(C++20) β specifies that a function is an immediate function, that is, every call to the function must be in a constant evaluation.constinitspecifier(C++20) β asserts that a variable has static initialization, i.e. zero initialization and constant initialization.
made with π and way too much group-chat energy by your fave manual-page bestie. source: http://cppreference.com; version/date: 2024.06.10; page: std::is_constant_evaluated(3). byeeee π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms