一、依據(jù)
C++標(biāo)準(zhǔn)規(guī)定:main函數(shù)可以省略返回語(yǔ)句,等效于返回0。
5. A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument.
If control reaches the end of main without encountering a return statement, the effect is that of executing
return 0;
——ISO C++03 3.6.1 Main function p44/72
——ISO C++98 3.6.1 Main function p43/69
注意:
1. main函數(shù)的返回類(lèi)型是int, 不是void或者其他類(lèi)型。
2. 該規(guī)則僅僅對(duì)main函數(shù)適用。
3. 對(duì)其他函數(shù),如果省略返回值, 將得到一個(gè)警告。
4. 應(yīng)該避免3的情況。
二、 示例
二、1. 一個(gè)合法的最小化的完整C++程序如下:
int main() {}
二、2. 省略的確切含義
同時(shí),標(biāo)準(zhǔn)中的用語(yǔ)是很
考究的:
“當(dāng)控制到達(dá)main結(jié)束處時(shí)沒(méi)有遇到return語(yǔ)句,效果與返回0相同”。
即是說(shuō),標(biāo)準(zhǔn)規(guī)定的是“對(duì)省略return的
分支,認(rèn)為返回0”。
同時(shí),標(biāo)準(zhǔn)也允許
其他分支含有返回語(yǔ)句。
如下:
int main(int argc,char* []) {
switch (argc)
{
case 1:
// error, should passing argument
return -1;
// parse arguments
default:
case 3: // parser argv[2]
case 2: // parser argv[1]
;
}
// do some work
// control reaches here
}
沒(méi)有輸入命令行參數(shù)時(shí), 返回一個(gè)錯(cuò)誤。
其他情況,當(dāng)控制達(dá)到main的
結(jié)尾處時(shí),效果同return 0;
二、3. 對(duì)于其他函數(shù),沒(méi)有這種“優(yōu)待”
如:
int not_main(int argc) {
if (argc<=1)
return -1;
}
int main(int argc,char* []) {
return not_main(argc);
}
not_main無(wú)疑將得到一個(gè)警告。
程序在沒(méi)有輸入命令行參數(shù)時(shí)的返回值將
無(wú)法預(yù)知。
三、驗(yàn)證
三、1. ERRORLEVEL
windows下,可以通過(guò) %
ERRORLEVEL% 查詢(xún)上一次程序返回值。
結(jié)果與判斷相吻合:
1. minimalist有
確定的返回值0
2. omit_return_in_main有
確定的返回值-1或0
3. 對(duì)omit_return_in_other
3.1 有命令行參數(shù)時(shí),返回值
確定為0。
3.2 無(wú)命令行參數(shù)時(shí),返回值
無(wú)法預(yù)知。
三、2 匯編代碼
更嚴(yán)謹(jǐn)?shù)尿?yàn)證方法是查看匯編代碼。
可以看到,在minimalist與omit_return_in_main的main函數(shù)中都有將eax置0的代碼。
在omit_return_in_other中的not_main函數(shù)中,沒(méi)有這樣的代碼。
四、 例外
VC6在這點(diǎn)上與標(biāo)準(zhǔn)不符。
四、1. 對(duì)omit_return_in_main,它給出的警告:
warning C4715: 'main' : not all control paths return a value
說(shuō)明它在這點(diǎn)上與標(biāo)準(zhǔn)不符。
顯然,在有命令行參數(shù)的時(shí)候,程序結(jié)果
無(wú)法預(yù)知。
四、2. 對(duì)minimalist,它給出的警告很搞笑:
warning C4508: 'main' : function should return a value;
'void' return type assumed暴露出它另一個(gè)
與標(biāo)準(zhǔn)不符的地方——main返回void。
顯然,任何情況下,程序結(jié)果都
無(wú)法預(yù)知。
四、3. 對(duì)omit_return_in_other,是程序員的錯(cuò)誤。
五、 實(shí)踐
不知道為什么C++標(biāo)準(zhǔn)在這里開(kāi)一個(gè)“后門(mén)”。
——C++在許多地方都是很
嚴(yán)謹(jǐn)的。
在實(shí)際應(yīng)用中,盡量不要采用這一特性, 因?yàn)椋?br>1. 舊編譯器不支持
2. C不支持——如果希望main能同時(shí)按C語(yǔ)言編譯的話(huà)
對(duì)于
演示用的C++代碼,與主題無(wú)關(guān)的代碼行能省則省,則可以使用這一特性。
比如:C++標(biāo)準(zhǔn)中的示例代碼幾乎都采用了這一特性。
由此可得出,C++標(biāo)準(zhǔn)在這里開(kāi)后門(mén)的原因是——讓C++標(biāo)準(zhǔn)更薄^_^
相關(guān)鏈接:
——示例代碼
http://immature.googlecode.com/svn/trunk/iMmature/sample/omit_return_in_main

本作品采用知識(shí)共享署名-非商業(yè)性使用-相同方式共享 2.5 中國(guó)大陸許可協(xié)議進(jìn)行許可。
轉(zhuǎn)載請(qǐng)注明 :
文章作者 - OwnWaterloo
發(fā)表時(shí)間 - 2009年04月26日
原文鏈接 - http://www.shnenglu.com/ownwaterloo/archive/2009/04/26/omit_return_in_main.html
posted on 2009-04-26 14:37
OwnWaterloo 閱讀(3474)
評(píng)論(1) 編輯 收藏 引用