c++ - constexpr 变量必须由 const 表达式初始化

标签 c++ c++14 constexpr

使用下面的代码,我得到Constexpr variable 'max_digits' must be initialized by a constant expression with Apple clang version 12.0.5 (clang-1205.0.22.11)运行 C++14。

但是,如果我#define WORKING_CODE 它可以正常工作,base_two_digits 是一个全局函数。如果我将 base_two_digits 的内容移动到 log_base_10 中,我会得到一个错误(这是当 #undef WORKING_CODE 存在时)。

如果 WORKING_CODE 未定义,为什么不是 constexpr

#include <limits>
#include <cstdint>
#include <cmath>

template<typename T>
constexpr T power_of_10(T n)
{
    return n < 0 ? 0 : n == 0 ? 1 : (n == 1 ? 10 : 10 * power_of_10(n - 1));
}

#ifdef WORKING_CODE
constexpr std::uint64_t base_two_digits(std::uint64_t value) {
    return value ? 8 * sizeof(value) - __builtin_clzll(value) : 0;
};
#endif

template<typename T>
constexpr T log_base_10(T value) {
    constexpr const unsigned guess[65] = {
        0 ,0 ,0 ,0 , 1 ,1 ,1 , 2 ,2 ,2 ,
        3 ,3 ,3 ,3 , 4 ,4 ,4 , 5 ,5 ,5 ,
        6 ,6 ,6 ,6 , 7 ,7 ,7 , 8 ,8 ,8 ,
        9 ,9 ,9 ,9 , 10,10,10, 11,11,11,
        12,12,12,12, 13,13,13, 14,14,14,
        15,15,15,15, 16,16,16, 17,17,17,
        18,18,18,18, 19
    };
    
    #ifdef WORKING_CODE
    T digits = guess[base_two_digits(value)];
    #else
    constexpr auto index = value ? 8 * sizeof(value) - __builtin_clzll(value) : 0;
    T digits = guess[index];
    #endif
    return digits + (value >= power_of_10(digits));
}

class BigInt {
private:
    static constexpr const std::uint32_t base = 1000;
    static constexpr const std::uint32_t max_digits = log_base_10(base) - 1; // 3;

public:
    BigInt() {}
};

int main() {
    BigInt p;

    return 0;
}

最佳答案

让我先简化代码(请忽略无意义的行为)到以下说明相同问题的示例:

template<typename T>
constexpr T log_base_10(T value) {
    constexpr auto index = value? 1:0;
    return 0;
}

int main() {
    constexpr auto foo = log_base_10(42);
    return 0;
}

这里的问题是使用 不一定 constexpr 本身的函数参数定义一个 constexpr 变量 index .如果 value 在运行时提供,则 index 的初始化将是不可能的。

WORKING_CODE 中不会出现此问题,因为

中的临时索引
T digits = guess[base_two_digits(value)];

没有被强制成为 constexpr(但在这种情况下可能是)。另一方面,

constexpr auto index = value ? 8 * sizeof(value) - __builtin_clzll(value) : 0;

是被迫的。

我不是 100% 确定编译器是否可以在这里给你一个通行证(w.r.t 标准),因为函数模板只为编译时调用实例化。但由于实例化的函数无法在运行时编译和调用,所以错误是有道理的。

解决方案是简单地使用普通的非constexpr index

关于c++ - constexpr 变量必须由 const 表达式初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68667348/

相关文章:

c++ - 数组 C++ 的 Ofstream 文本编写问题

c++ - 为什么我这里不需要SFINAE

具有嵌套表达式模板的 C++ 类

c++ - C++ 中用于自动将对象转换为 "printable"格式的重载歧义

c++ - 将多字节 ASCII 文字分配给枚举值

c++ - constexpr 构造函数初始化列表中的 Constexpr 函数

c++ - 在 SFML 中不使用 Sprite 显示图像

c++ - 命名空间标识符是否应该放在每个参数的前面?

C++多重继承函数合并

c++ - 我对 constexpr 函数感到困惑?