C++核心准则边译边学-P.2 使用ISO标准C++写代码

P.2: Write in ISO Standard C++(使用ISO标准C++写代码)

Reason(原因)

This is a set of guidelines for writing ISO Standard C++.

这是一套如何写ISO标准C++代码的指南。

译者注:不知道作者是不是开玩笑。似乎这样的答案更靠谱:ISO Standard C++会给你带来。。。

Note(注意事项)

There are environments where extensions are necessary, e.g., to access system resources. In such cases, localize the use of necessary extensions and control their use with non-core Coding Guidelines.  If possible, build interfaces that encapsulate the extensions so they can be turned off or compiled away on systems that do not support those extensions.

在有的ISO标准C++环境需要扩展,例如在访问系统资源的时候。在这种情况下,定位使用必要扩展的位置并使用非核心编码指南控制他们。如果可能,构建封装这些扩展的接口,这样就可以在不支持这些扩展的系统上关闭它们或者将它们排除在编译范围之外。

Extensions often do not have rigorously defined semantics.  Even extensions that are common and implemented by multiple compilers may have slightly different behaviors and edge case behavior as a direct result of not having a rigorous standard definition.  With sufficient use of any such extension, expected portability will be impacted.

扩展经常没有严格定义的语义。由于这个原因,哪怕是那些常见的,被多种编译支持的扩展的行为也会稍有不同,也会存在边界行为。由于这种扩展的大量使用,我们期待的可移植性会受到损害。

Note(注意事项)

Using valid ISO C++ does not guarantee portability (let alone correctness). Avoid dependence on undefined behavior (e.g., undefined order of evaluation) and be aware of constructs with implementation defined meaning (e.g., sizeof(int)).

使用合法的ISO C++不能保证可移植性(更别说正确性了)。避免依靠无定义的行为(例如,没有被定义的演算顺序)并且注意哪些可能受实现方式影响的的概念(例如,sizeof(int))。

译者注:估计作者想表达的意思是,在不同的编译环境下int的长度可能是4个字节或者其他长度,因此sizeof的结果可能就会不同。再有就是不要认为只要用了ISO C++就万事大吉了,还有很多地方要注意。

 

Note(注意事项)

There are environments where restrictions on use of standard C++ language or library features are necessary, e.g., to avoid dynamic memory allocation as required by aircraft control software standards. In such cases, control their (dis)use with an extension of these Coding Guidelines customized to the specific environment.

有些环境使用标准C++语言或必要的库功能会受限,例如,飞机控制软件标准就会要求避免使用动态内存分配。这种情况下,可以先为特殊环境定制本指南的扩展,然后在使用它控制语言或库功能的使用(或不使用)。

Enforcement(实施建议)

Use an up-to-date C++ compiler (currently C++17, C++14, or C++11) with a set of options that do not accept extensions.

配合一套不接受扩展的选项使用最新的C++编译器(现在是C++17,C++14或者C++11)。

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值