前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则:积极、正确地使用标准库

C++核心准则:积极、正确地使用标准库

作者头像
面向对象思考
发布2020-10-30 11:28:27
3000
发布2020-10-30 11:28:27
举报

SL.1: Use libraries wherever possible

SL.1:在任何可能的情况下使用已有库

Reason(原因)

Save time. Don't re-invent the wheel. Don't replicate the work of others. Benefit from other people's work when they make improvements. Help other people when you make improvements.

节约时间。不要重新发明车轮。不要重复其他人的工作。如果其他人进行了某种改善,我们可以从中获益。如果你进行了某种改善,帮助其他人获益。

SL.2: Prefer the standard library to other libraries

SL.2:标准库好于其他库

Reason(原因)

More people know the standard library. It is more likely to be stable, well-maintained, and widely available than your own code or most other libraries.

更多人知道标准库。标准库更有可能维持稳定,被很好地维护,比你自己写的代码或其他库具有更广泛的可用性。

SL.3: Do not add non-standard entities to namespace std

SL.3:不用选项std命名空间增加非标准实体

Reason(原因)

Adding to std might change the meaning of otherwise standards conforming code. Additions to std might clash with future versions of the standard.

将实体添加到std命名空间可能会改变其他遵守标准的代码的含义。增加到std命名空间中的实体有可能和标准库的将来版本发生冲突。

Example(示例)

代码语言:javascript
复制
???
Enforcement(实施建议)

Possible, but messy and likely to cause problems with platforms.

有可能,但是会混乱并可能引起平台相关问题。

SL.4: Use the standard library in a type-safe manner

SL.4:以类型安全的方式使用标准库

Reason(原因)

Because, obviously, breaking this rule can lead to undefined behavior, memory corruption, and all kinds of other bad errors.

因为,很显然,破坏这条规则会引发无定义行为,内存破坏或者各种其他类型的恶劣问题。

Note(注意)

This is a semi-philosophical meta-rule, which needs many supporting concrete rules. We need it as an umbrella for the more specific rules.

这是一条半哲学性质的准则,它需要很多具体准则的支持。我们需要将本规则当作其它具体规则的整体概括来看。

Summary of more specific rules:

更特定规则的归纳:

  • SL.4: Use the standard library in a type-safe manner
  • SL.4: 以类型安全的方式使用标准库

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sl1--use-libraries-wherever-possible

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

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

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

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

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