0
点赞
收藏
分享

微信扫一扫

C++核心准则边译边学-F.46 main函数的返回值类型是整数


F.46: ​​int​​​ is the return type for ​​main()​

F.46:main函数的返回值类型是整数

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.

这是语言本身的准则,但是由于通过“语言扩展”的方式违反该准则的情况非常常见,因此有必要提醒。将main函数(程序唯一的全局main函数)定义为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.

我们提醒这一点只是因为在社区中存在对这个错误的坚持。

Enforcement(实施建议)

  • The compiler should do it
    编译器会检查。
  • If the compiler doesn't do it, let tools flag it
    如果编译器不检查,让工具检查。

 


阅读更多更新文章,请关注微信公众号【面向对象思考】

举报

相关推荐

0 条评论