主要的递归

use*_*638 4 c++ recursion

我读了一些在c ++中不允许使用main()的递归的地方,但是当我尝试它时没有任何错误

#include<iostream>
using namespace std;

int i=10;

int main()
{
    if(i==1)
    {
        cout<<i;
        return 0;
    }
    i--;
    main();
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 11

main显式调用是未定义的行为,任何事情都可能发生(包括似乎工作).

C++ 03 3.6.1

3)函数main不得在程序中使用.[...]

编译器(作为所有未定义的行为)不需要提供诊断,也不需要运行时崩溃.