Namespaces
Variants

std::is_constant_evaluated

来自 zh.cppreference.net
 
 
 
定义于头文件 <type_traits>
constexpr bool is_constant_evaluated() noexcept;
(C++20 起)

检测函数调用是否发生在常量求值语境中。如果调用求值发生在显式常量求值的表达式或转换的求值过程中,返回 true;否则返回 显式常量求值。 false为确定以下变量的初始化器是否显式常量求值,编译器可能首先执行一次试探性常量求值:

引用类型或 const 限定的整数或枚举类型的变量;

  • 静态及线程局部变量。
  • 不建议在此情况下依赖其结果。

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

参数

(无)

返回值

若调用的求值发生在明显常量求值的表达式或转换的求值过程中,则为 true ;否则为 false 。

可能的实现

// 此实现需要 C++23 的 if consteval 特性
constexpr bool is_constant_evaluated() noexcept
{
    if consteval
    {
        return true;
    }
    else 
    {
        return false;
    }
}

注释

当直接用作 static_assert 声明或 constexpr if语句 的条件时, std :: is_constant_evaluated ( ) 始终返回 true 。

由于 C++20 中缺少 if consteval , std::is_constant_evaluated 通常通过编译器扩展来实现。

功能测试 宏 值 标准 功能
__cpp_lib_is_constant_evaluated 201811L (C++20) std::is_constant_evaluated

示例

#include <cmath>
#include <iostream>
#include <type_traits>
constexpr double power(double b, int x)
{
    if (std::is_constant_evaluated() && !(b == 0.0 && x < 0))
    {
        // 常量求值上下文:使用 constexpr 友好算法
        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
    {
        // 交由代码生成器处理
        return std::pow(b, double(x));
    }
}
int main()
{
    // 常量表达式上下文
    constexpr double kilo = power(10.0, 3);
    int n = 3;
    // 非常量表达式,因为 n 在常量表达式上下文中无法转换为右值
    // 等价于 std::pow(10.0, double(n))
    double mucho = power(10.0, n);
    std::cout << kilo << " " << mucho << "\n"; // (3)
}

输出:

1000 1000

参见

constexpr specifier (C++11) 指定变量或函数的值可在编译时计算
consteval specifier (C++20) 指定函数为 立即函数 ,即对该函数的每次调用都必须在常量求值中进行
constinit specifier (C++20) 断言变量具有静态初始化,即 零初始化 和 常量初始化