C++ 如果未指定返回类型,则递归lambda无法编译

C++ 如果未指定返回类型,则递归lambda无法编译,c++,recursion,lambda,c++14,C++,Recursion,Lambda,C++14,以下代码未能编译(在gcc9.2中): #包括 int main(){ 自动函数=[](自动函数,int n){ std::cout void{ std::cout当我尝试编译此文件时,我得到以下消息: main.cpp: In instantiation of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]': main.cpp:10:17: required

以下代码未能编译(在gcc9.2中):

#包括
int main(){
自动函数=[](自动函数,int n){
std::cout void{

std::cout当我尝试编译此文件时,我得到以下消息:

main.cpp: In instantiation of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]':
main.cpp:10:17:   required from here
main.cpp:7:18: error: use of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]' before deduction of 'auto'
    7 |             _func(_func, n - 1);
      |             ~~~~~^~~~~~~~~~~~~~
main.cpp:In'main()::[with auto:1=main():]的实例化中:
main.cpp:10:17:从这里开始需要
main.cpp:7:18:错误:在扣除“auto”之前使用“main()::[with auto:1=main()::]”
7 | func(_func,n-1);
|             ~~~~~^~~~~~~~~~~~~~
关键是

错误:在扣除“自动”之前使用“lambda”

一个更简单的例子是

#包括
自动f(整数x){

std::cout您会得到什么错误?可能是因为编译器无法从函数中递归推断类型,因为签名依赖于自身。这就像一个循环依赖项,因此它将解析为未定义的行为。声明类型将打破发现签名的循环。
#include<iostream>

int main () {
    auto func = [](auto _func, int n)->void {
        std::cout << n << '\n';
        if (n > 1) {
            _func(_func, n - 1);
        }
    };
    func(func, 3);
}
<source>: In instantiation of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]':

<source>:10:17:   required from here

<source>:7:18: error: use of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]' before deduction of 'auto'

    7 |             _func(_func, n - 1);

      |             ~~~~~^~~~~~~~~~~~~~

ASM generation compiler returned: 1
main.cpp: In instantiation of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]':
main.cpp:10:17:   required from here
main.cpp:7:18: error: use of 'main()::<lambda(auto:1, int)> [with auto:1 = main()::<lambda(auto:1, int)>]' before deduction of 'auto'
    7 |             _func(_func, n - 1);
      |             ~~~~~^~~~~~~~~~~~~~