为什么此代码在 Visual C++ 中会产生以下错误?
是编译器的错误还是代码无效?
template<int N> int test(int = sizeof(test<N - 1>()));
template<> int test<0>(int);
int main() { return sizeof(test<1>()); }
Recursive type or function dependency context too complex
最佳答案
test 在您使用它时尚未声明。 C++11 中经常出现类似的问题:
template<int N> auto test() -> decltype(test<N - 1>());
template<> auto test<0>() -> int;
int main() { return sizeof(test<1>()); }
有讨论在未来改变它。可编译的代码版本:
template<int N> int test(int);
template<> int test<0>(int);
template<int N> int test() { return test<N>(sizeof(test<N - 1>())); }
int main() { return sizeof(test<1>()); }
关于c++ - 为什么我得到 "recursive type or function dependency context too complex"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14212818/