ES.49: If you must use a cast, use a named cast
ES.49:如果必须进行类型转换,使用命名转换
Reason(原因)
Readability. Error avoidance. Named casts are more specific than a C-style or functional cast, allowing the compiler to catch some errors.
可读性。避免错误。命名转换比C风格转换或函数形式转换更明确,允许编译器捕捉更多错误。
The named casts are:
命名转换包括:
- static_cast
- 静态转换
- const_cast
- 常量转换
- reinterpret_cast
- 重新解释转换
- dynamic_cast
- 动态转换
- std::move // move(x) is an rvalue reference to x
- 移动//move(x)是x的右值引用
- std::forward // forward<T>(x) is an rvalue or an lvalue reference to x depending on T
- 值性转递//根据T的类型,forward<T>(x)是左值或右值引用
- gsl::narrow_cast // narrow_cast<T>(x) is static_cast<T>(x)
- 窄化转换//narrow_cast<T>(x)就是static_cast<T>(x)
- gsl::narrow // narrow<T>(x) is static_cast<T>(x) if static_cast<T>(x) == x or it throws narrowing_error
- 窄化转换(抛出异常)//如果static_cast<T>(x) == x,narrow<T>(x) 就是 static_cast<T>(x),否则抛出异常
Example(示例)
The example was synthesized from real-world bugs where D used to be derived from B, but someone refactored the hierarchy. The C-style cast is dangerous because it can do any kind of conversion, depriving us of any protection from mistakes (now or in the future).
示例是从实际代码中收集的的错误集合,这段代码的前提是D过去继承于B,但有人重构了继承关系。C风格转换的危险性来自它可以是任何类型的转换,这抹杀了任何防错保护的可能性(无论是现在还是未来)。
Note(注意)
When converting between types with no information loss (e.g. from float to double or from int32 to int64), brace initialization may be used instead.
如果希望在类型之间进行无损转换(例如从float到double,或者从int32到int64),可以考虑转而使用大括号初始化。
This makes it clear that the type conversion was intended and also prevents conversions between types that might result in loss of precision. (It is a compilation error to try to initialize a float from a double in this fashion, for example.)
这种方式一方面明确了类型转换的意图,另一方面可以防止转换时损失精度。(例如,在如代码所示的情况下,如果使用double值初始化float变量,会发生编译错误)
Note(注意)
reinterpret_cast can be essential, but the essential uses (e.g., turning a machine address into pointer) are not type safe:
reinterpret_cast是必不可少的,但是这种必要的用法(例如,将机器地址转换为指针)不是类型安全的。
Enforcement(实施建议)
- Flag C-style and functional casts.
- 对C风格和函数形式转换进行提醒
- The type profile bans reinterpret_cast.
- 类型规则群组禁止reinterpret_cast.
- The type profile warns when using static_cast between arithmetic types.
- 类型规则群组对在算数类型之间进行转换时使用static_cast的情况进行警告。
译者注:
C风格转换:b = int(a);
函数形式转换:b=int(a);
原文链接
https:///isocpp/CppCoreGuidelines/blob/master/#es49-if-you-must-use-a-cast-use-a-named-cast
觉得本文有帮助?欢迎点赞并分享给更多的人。
阅读更多更新文章,请关注微信公众号【面向对象思考】
 
 
                     
            
        













 
                    

 
                 
                    