是否存在这样的编译时计算,可以让编译器进入无休止的循环?
无休止的循环可以不消耗不断增加的内存吗?或者它可能会因为缺乏记忆而停止。
与运行时一样,存在无限循环,但与运行时不同的是,编译器将在足够的迭代后停止。这里有一个例子:
#include <iostream>
template<int i>
struct loop {
// this will just keep referencing itself
static constexpr int val = loop<i + 1>::val;
};
int main() {
std::cout << loop<0>::val << std::endl;
}
编译器给出以下错误消息:
fatal error: template instantiation depth exceeds maximum of 900