前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则​Pro.safety:类型安全准则群组

C++核心准则​Pro.safety:类型安全准则群组

作者头像
面向对象思考
发布2020-11-10 10:59:41
5570
发布2020-11-10 10:59:41
举报

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.6:始终初始化成员变量:始终初始化,可能使用默认构造函数默认成员初始化器。
  • 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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Pro.safety: Type-safety profile
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档