不限制递归深度,就可以对编译器发起DoS attack
MSVC的demo可以用这个程序(
https://itecnote.com/tecnote/c-template-metaprogramming-recursion-up-limits/)
VS2022的/std:c++20只能编译Sum<499>::value,不能编译Sum<500>::value,跟帖子里说的VC9一样。
template <int N> class Sum {
public:
enum { value = N + Sum<N - 1>::value };
};
template <> class Sum<0> {
public:
enum { value = 0 };
};
int main() {
printf("%d\n", Sum<499>::value);
The standard recommends an implementation support at least 1024 levels of recursive instantiation, and infinite recursion in template instantiations is undefined behavior.
https://en.cppreference.com/w/cpp/language/template_metaprogramming
--
修改:z16166 FROM 221.220.168.*
FROM 221.220.168.*