C++核心准则Pro.safety:类型安全准则群组
Pro.safety: Type-safety profile
Pro.safety:类型安全准则群组
This profile makes it easier to construct code that uses types correctly and avoids inadvertent type punning. It does so by focusing on removing the primary sources of type violations, including unsafe uses of casts and unions.
此群组使程序员可以更容易地构建类型正确的代码,并且避免了无意的类型调整。它的重点是消除类型违规的主要根源,包括不安全使用强制类型转换和联合。
For the purposes of this section, type-safety is defined to be the property that a variable is not used in a way that doesn't obey the rules for the type of its definition. Memory accessed as a type T should not be valid memory that actually contains an object of an unrelated type U. Note that the safety is intended to be complete when combined also with Bounds safety and Lifetime safety.
在本节中,类型安全性的含义是:变量的属性没有以违反该变量被定义时的类型规则的方式被使用。作为类型T访问的内存不应是包含实际上是无关类型U的对象的有效内存。请注意,将安全性与“边界”安全性和“生命周期”安全性结合使用时,应确保其安全性。
An implementation of this profile shall recognize the following patterns in source code as non-conforming and issue a diagnostic.
Type safety profile summary:
此规则群组的实现应将源代码中的以下模式识别为不合格,并发出诊断信息。
类型安全群组概要:
Type.1: Avoid casts: a. Don't use reinterpret_cast; A strict version of Avoid casts and prefer named casts. b. Don't use static_cast for arithmetic types; A strict version of Avoid casts and prefer named casts. c. Don't cast between pointer types where the source type and the target type are the same; A strict version of Avoid casts. d. Don't cast between pointer types when the conversion could be implicit; A strict version of Avoid casts.
类型1:避免强制转换:不要使用reinterpret_cast;避免使用强制转换的严格版本,最好使用命名转换。b.不要将static_cast用于算术类型;避免使用强制转换的严格版本,最好使用命名转换。C。不要在源类型和目标类型相同的指针类型之间进行强制转换;避免使用强制转换的严格版本。d。当转换可能是隐式的时,不要在指针类型之间进行转换;避免使用强制转换的严格版本。
Type.2: Don't use static_cast to downcast: Use dynamic_cast instead.
类型2:请勿使用static_cast进行下转换:请使用dynamic_cast。
Type.3: Don't use const_cast to cast away const (i.e., at all): Don't cast away const.
类型3:请勿使用const_cast舍弃const(即完全取消):请勿舍弃const。
Type.4: Don't use C-style (T)expression or functional T(expression) casts: Prefer construction or named casts or T{expression}.
Type.4:请勿使用C样式(T)表达式或函数型T(expression)强制类型转换:构造或命名强制类型转换或T {expression}更好。
Type.5: Don't use a variable before it has been initialized: always initialize.
Type.5:初始化之前不要使用变量:始终初始化。
Type.6: Always initialize a member variable: always initialize, possibly using default constructors or default member initializers.
Type.7: Avoid naked union: Use variant instead.
类型7:避免暴露的联合体:改用标准库variant类型。
Type.8: Avoid varargs: Don't use va_arg arguments.
Type.8:避免使用varargs:不要使用va_arg参数。
Impact(影响)
With the type-safety profile you can trust that every operation is applied to a valid object. An exception can be thrown to indicate errors that cannot be detected statically (at compile time). Note that this type-safety can be complete only if we also have Bounds safety and Lifetime safety. Without those guarantees, a region of memory could be accessed independent of which object, objects, or parts of objects are stored in it.
使用类型安全规则群组,您可以相信每个操作都将应用于有效对象。可以引发异常以指示无法静态(在编译时)检测到的错误。请注意,只有在我们同时确保边界安全性和生命周期安全性的情况下,才能b此类型安全性。如果没有这些保证,则可以任意访问存储区域,而与存储在其中的个别对象,全体对象或部分对象无关。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#prosafety-type-safety-profile
新书介绍
《实战Python设计模式》是作者最近出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!