前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则编译边学-F.46 main函数的返回值类型是整数

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

作者头像
面向对象思考
发布2020-03-25 15:32:52
6750
发布2020-03-25 15:32:52
举报

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(示例)
代码语言:javascript
复制
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 如果编译器不检查,让工具检查。

觉得本文有帮助?请分享给更多人。

关注【面向对象思考】,轻松学习每一天!

有任何疑问,欢迎留言提问或讨论。


面向对象设计,面向对象编程,面向对象思考!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • F.46: int is the return type for main()
    • Reason(原因)
      • Example(示例)
        • Note(注意)
          • Enforcement(实施建议)
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档