Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我会得到;递归类型或函数依赖项上下文太复杂;? 为什么这个代码在VisualC++中产生以下错误? 这是编译器中的错误还是代码无效 template<int N> int test(int = sizeof(test<N - 1>())); template<> int test<0>(int); int main() { return sizeof(test<1>()); } 模板int测试(int=sizeof(test()); 模板int测试(int); int main(){return sizeof(test());}_C++_Templates_Visual C++ - Fatal编程技术网

为什么我会得到;递归类型或函数依赖项上下文太复杂;? 为什么这个代码在VisualC++中产生以下错误? 这是编译器中的错误还是代码无效 template<int N> int test(int = sizeof(test<N - 1>())); template<> int test<0>(int); int main() { return sizeof(test<1>()); } 模板int测试(int=sizeof(test()); 模板int测试(int); int main(){return sizeof(test());}

为什么我会得到;递归类型或函数依赖项上下文太复杂;? 为什么这个代码在VisualC++中产生以下错误? 这是编译器中的错误还是代码无效 template<int N> int test(int = sizeof(test<N - 1>())); template<> int test<0>(int); int main() { return sizeof(test<1>()); } 模板int测试(int=sizeof(test()); 模板int测试(int); int main(){return sizeof(test());},c++,templates,visual-c++,C++,Templates,Visual C++,递归类型或函数依赖项上下文太复杂 测试尚未在您使用它时声明。C++11中经常出现类似的问题: template<int N> auto test() -> decltype(test<N - 1>()); template<> auto test<0>() -> int; int main() { return sizeof(test<1>()); } template auto test()->decltype(test

递归类型或函数依赖项上下文太复杂


测试尚未在您使用它时声明。C++11中经常出现类似的问题:

template<int N> auto test() -> decltype(test<N - 1>());
template<> auto test<0>() -> int;
int main() { return sizeof(test<1>()); }
template auto test()->decltype(test());
模板自动测试()->int;
int main(){return sizeof(test());}
有人在讨论将来如何改变这一点。编译以下内容的代码版本:

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>()); }
模板int测试(int);
模板int测试(int);
模板int test(){返回测试(sizeof(test());}
int main(){return sizeof(test());}

干得好。你现在比电脑还先进。你想达到什么目的?
test
的默认参数是
sizeof(test())
,这要求确定
test
的默认参数,即
sizeof(test())
,如果您还没有超过编译器限制,那么您将得到从
test
的所有实例化。(编辑:这实际上是Xeo在评论Marc Glisse的回答时指出的。)+1。我本来打算在看到后再发这封信的PAnd显而易见的解决方案是向前声明它<代码>模板int测试(int)。您可以在以后的声明/定义中添加默认参数,因此这不是问题。。我想知道为什么@纳瓦兹:我注意到了,我可以发誓你以后可以添加默认参数。。。哦,等等,我知道问题出在哪里了。这种代码永远无法工作。默认参数尝试调用它作为默认参数的函数,并且不传递任何参数。
test
然后尝试使用默认参数,从而在声明中不确定地递归。@Nawaz:嗯。。。您不再使用默认参数,因此忽略OP试图实现的目标…:P