前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则​CPL.2:如果你必须使用C,使用C和C++的共同子集,并且使用C++编译器编译C代码

C++核心准则​CPL.2:如果你必须使用C,使用C和C++的共同子集,并且使用C++编译器编译C代码

作者头像
面向对象思考
发布2020-10-10 10:03:04
6890
发布2020-10-10 10:03:04
举报
文章被收录于专栏:C++核心准则原文翻译

CPL.2: If you must use C, use the common subset of C and C++, and compile the C code as C++

CPL.2:如果你必须使用C,使用C和C++的共同子集,并且使用C++编译器编译C代码

Reason(原因)

That subset can be compiled with both C and C++ compilers, and when compiled as C++ is better type checked than "pure C."

这样的子集C和C++都可以编译通过,而且作为C++代码编译时获得比“纯C”更好的类型检查。

Example(示例)

代码语言:javascript
复制
int* p1 = malloc(10 * sizeof(int));                      // not C++
int* p2 = static_cast<int*>(malloc(10 * sizeof(int)));   // not C, C-style C++
int* p3 = new int[10];                                   // not C
int* p4 = (int*) malloc(10 * sizeof(int));               // both C and C++
Enforcement(实施建议)
  • Flag if using a build mode that compiles code as C. 标记将代码按照C编译的情况。
    • The C++ compiler will enforce that the code is valid C++ unless you use C extension options. 除非你使用了C扩展选项,C++编译器会强制代码符合C++规范。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cpl2-if-you-must-use-c-use-the-common-subset-of-c-and-c-and-compile-the-c-code-as-c

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reason(原因)
    • Enforcement(实施建议)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档