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等。敬请期待。

 

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

阅读更多更新文章,请关注微信公众号【面向对象思考】