【发布时间】:2020-08-04 14:48:49
【问题描述】:
我正在尝试做类似的事情:
#include <cmath>
template <unsigned A, unsigned B>
struct fu {
constexpr unsigned long power_A_of_B = std::pow(A, B);
};
但是编译器会报错,因为pow() 不是constexpr。
为什么标题 <cmath> 中的数学函数不是 constexpr?
例如,log()、log2()、pow()、abs() 和fmax() 不是constexpr,但<algorithm> 中的其他人是,例如max()、min() 和`clamp()' .
【问题讨论】:
-
你可以自己做
-
std::is_constant_evaluated自己做 :) -
答案不是一些深层的技术原因,只是“委员会只是人类,他们还没有达到它”。不确定these proposals 的当前状态。
-
可能重复:Constexpr Math Functions。简而言之,许多
cmath函数设置errno,这是一个副作用,因此是非constexpr。
标签: c++ templates constexpr c++20