constexpr函数中使用循环

从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告知。

posted @ 2024-09-28 14:05  寻找繁星  阅读(0)  评论(0编辑  收藏  举报