C++核心准则边译边学-P.4 理想情况下,程序应该静态类型安全

网友投稿 998 2022-11-12

C++核心准则边译边学-P.4 理想情况下,程序应该静态类型安全

C++核心准则边译边学-P.4 理想情况下,程序应该静态类型安全

P.4: Ideally, a program should be statically type safe(理想情况下,程序应该静态安全)

Reason(原因)

Ideally, a program would be completely statically (compile-time) type safe. Unfortunately, that is not possible. Problem areas:

理想情况下,程序是完全的静态(编译时)类型安全的。但不幸的是,这是不可能的。问题领域包括:

。unions(联合体)

。casts(类型转换)

。array decay(数组破坏)

。range errors(范围错误)

。narrowing conversions(窄化变换)

Note(注意事项)

These areas are sources of serious problems (e.g., crashes and security violations). We try to provide alternative techniques.

这些领域是严重错误的源泉(例如,崩溃和安全违反)。我们力争提供替代技术。

Enforcement(实施建议)

We can ban, restrain, or detect the individual problem categories separately, as required and feasible for individual programs. Always suggest an alternative. For example:

对于单独的程序,分别禁止,限制,或者检查单独的各个问题分类是必要且可行的。我们也会提供代替方案,例如:

。unions -- use ​​variant​​ (in C++17)

联合--使用C++17的variant

。casts -- minimize their use; templates can help

类型转换--尽量减少使用他们,模板可以提供帮助

。array decay -- use ​​span​​ (from the GSL)

数组破坏--使用span(来自GSL)

。range errors -- use ​​span​​

​​  范围作物--使用span​​

。narrowing conversions -- minimize their use and use ​​narrow​​​ or ​​narrow_cast​​ (from the GSL) where they are necessary

窄化转换--尽量减少使用类型转换并且在必要时使用narrow或者narrow_cast(来自GSL)

后续文章将会介绍本文提到的相关技术:variant,span,narrow和narrow_cast等。敬请期待。

觉得本文有帮助,欢迎点赞并分享给您的朋友。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Tkinter编程应知应会(14)-文本编辑控件Text
下一篇:Spring Cache 集成 Caffeine实现项目缓存的示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~