<分区>
constexpr PixelType maxVal = std::numeric_limits<PixelType>::max();
constexpr double lnFactor = std::log(maxVal);
第二行给出错误 C2131:表达式未计算为常量
为什么?可以改写它以便编译吗?
标签 c++ c++11 visual-c++ c++14
<分区>
constexpr PixelType maxVal = std::numeric_limits<PixelType>::max();
constexpr double lnFactor = std::log(maxVal);
第二行给出错误 C2131:表达式未计算为常量
为什么?可以改写它以便编译吗?
最佳答案
编译错误的原因是std::log
不是constexpr
。
我不知道有一种可移植的方式让它与 constexpr
一起工作,但是 GCC 有 __builtin_log
可以在 constexpr
上下文中工作(godbolt ):
constexpr double lnFactor = __builtin_log(42.0);
关于c++ - 为什么 constexpr 的标准库函数不是常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774030/