C++核心準(zhǔn)則編譯邊學(xué)-F.46 main函數(shù)的返回值類型是整數(shù)

F.46: int is the return type for main()
F.46:main函數(shù)的返回值類型是整數(shù)
Reason(原因)
It's a language rule, but violated through "language extensions" so often that it is worth mentioning.
Declaring main (the one global main of a program) void limits portability.
這是語言本身的準(zhǔn)則,但是由于通過“語言擴(kuò)展”的方式違反該準(zhǔn)則的情況非常常見,因此有必要提醒。將main函數(shù)(程序唯一的全局main函數(shù))定義為void會限制移植性。
Example(示例)
void?main()?{?/*?...?*/?};??//?bad,?not?C++int?main(){std::cout << "This is the way to do it\n";}
Note(注意)
We mention this only because of the persistence of this error in the community.
我們提醒這一點只是因為在社區(qū)中存在對這個錯誤的堅持。
Enforcement(實施建議)
The compiler should do it
編譯器會檢查。
If the compiler doesn't do it, let tools flag it
如果編譯器不檢查,讓工具檢查。
覺得本文有幫助?請分享給更多人。
關(guān)注【面向?qū)ο笏伎肌?,輕松學(xué)習(xí)每一天!
有任何疑問,歡迎留言提問或討論。
面向?qū)ο笤O(shè)計,面向?qū)ο缶幊?,面向?qū)ο笏伎迹?/p>
評論
圖片
表情
