ES.49:如果必须进行类型转换,使用命名转换
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:
命名转换包括:
class B { /* ... */ };
class D { /* ... */ };
template<typename D> D* upcast(B* pb)
{
D* pd0 = pb; // error: no implicit conversion from B* to D*
D* pd1 = (D*)pb; // legal, but what is done?
D* pd2 = static_cast<D*>(pb); // error: D is not derived from B
D* pd3 = reinterpret_cast<D*>(pb); // OK: on your head be it!
D* pd4 = dynamic_cast<D*>(pb); // OK: return nullptr
// ...
}
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),可以考虑转而使用大括号初始化。
double d {some_float};
int64_t i {some_int32};
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是必不可少的,但是这种必要的用法(例如,将机器地址转换为指针)不是类型安全的。
auto p = reinterpret_cast<Device_register>(0x800); // inherently dangerous
译者注:
C风格转换:b = int(a);
函数形式转换:b=int(a);
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es49-if-you-must-use-a-cast-use-a-named-cast