constexpr函数中使用循环

阅读量: searchstar 2021-12-27 22:55:23
Categories: Tags:

从C++14开始,constexpr函数中也可以使用循环了。

constexpr int pow(int a, int b) {
int ans = 1;
while (b--) {
ans *= a;
}
return ans;
}
static_assert(pow(3, 4) == 81);

感谢@deepbluev7:neko.dev告知。