c++ - 为什么我得到 "recursive type or function dependency context too complex"?

标签 c++ templates visual-c++

为什么此代码在 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/

相关文章:

c++ - 在 C++ 中查找字符串中的子字符串标记

c++ - "template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };"是什么意思,它如何与 std::visit 一起使用?

c++ - 在 Visual Studio 2015 中从源代码构建 OGRE3D 时如何解决错误

c++ - 比较短和长是隐式转换吗?

c++ - 元组的运行时索引

c++ - msvcr90.dll 有时间戳吗?

c++ - 如何在增量循环中构造 std::list 迭代器

c++ - 从字符串c++的开头和结尾删除标点符号

javascript - 如果无法分配内存,V8 会崩溃吗?这会使整个过程崩溃吗?

c++ - 如何避免在调用此模板时必须使用decltype?