本文介绍了是main()真的开始一个C ++程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准中的$ 3.6.1 / 1部分显示为

现在考虑这个代码,

  int square(int i){return i * i; } 
int user_main()
{
for(int i = 0; i std :: cout<平方(i) endl;
return 0;
}
int main_ret = user_main();
int main()
{
return main_ret;
}

此示例代码执行我打算执行的操作,即打印从0到9的整数,之前输入应该是程序开始的 main() >

请看这里的输出:



我也使用 -pedantic 选项,GCC 4.5.0编译。



所以我的问题是,



是这个代码真的符合标准吗?



如果标准符合标准,那么标准是否符合标准? main()不是此程序的启动! main()之前执行 user_main()



我知道要初始化全局变量 main_ret use_main code>首先执行,但这是一个不同的事情;关键是,确实使标准中引用的语句$ 3.6.1 / 1无效,因为 main()不是开始;它实际上是程序的结束






编辑:



如何定义单词'start'?



短语程序开始。那么你如何定义它呢?

解决方案

不,C ++做了很多事情来设置环境主调用然而,main是C ++程序的用户指定部分的官方开始。



一些环境设置是不可控的(如初始代码设置std :: cout;但是,一些环境是可控的,像静态全局块(用于初始化静态全局变量)。注意,由于你在main之前没有完全控制,你不能完全控制顺序



在main之后,你的代码在概念上是完全在控制的程序,在这个意义上,你可以指定要执行的指令多线程可以重新排列代码执行顺序;但是,你仍然可以用C ++控制,因为你指定了代码部分执行(可能)乱序。


The section $3.6.1/1 from the C++ Standard reads,

Now consider this code,

int square(int i) { return i*i; }
int user_main()
{
    for ( int i = 0 ; i < 10 ; ++i )
           std::cout << square(i) << endl;
    return 0;
}
int main_ret= user_main();
int main()
{
        return main_ret;
}

This sample code does what I intend it to do, i.e printing the square of integers from 0 to 9, before entering into the main() function which is supposed to be the "start" of the program.

Have a look at the output here : http://www.ideone.com/Niy0R

I also compiled it with -pedantic option, GCC 4.5.0. It gives no error, not even warning!

So my question is,

Is this code really Standard conformant?

If it's standard conformant, then does it not invalidate what the Standard says? main() is not start of this program! user_main() executed before the main().

I understand that to initialize the global variable main_ret, the use_main() executes first but that is a different thing altogether; the point is that, it does invalidate the quoted statement $3.6.1/1 from the Standard, as main() is NOT the start of the program; it is in fact the end of this program!


EDIT:

How do you define the word 'start'?

It boils down to the definition of the phrase "start of the program". So how exactly do you define it?

解决方案

No, C++ does a lot of things to "set the environment" prior to the call of main; however, main is the official start of the "user specified" part of the C++ program.

Some of the environment setup is not controllable (like the initial code to set up std::cout; however, some of the environment is controllable like static global blocks (for initializing static global variables). Note that since you don't have full control prior to main, you don't have full control on the order in which the static blocks get initialized.

After main, your code is conceptually "fully in control" of the program, in the sense that you can both specify the instructions to be performed and the order in which to perform them. Multi-threading can rearrange code execution order; but, you're still in control with C++ because you specified to have sections of code execute (possibly) out-of-order.

这篇关于是main()真的开始一个C ++程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:41