mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Clarify "unnecessary cast" by decomposing the class into identity casts and implicit casts.
This commit is contained in:
@@ -11620,7 +11620,7 @@ If you feel the need for a lot of casts, there may be a fundamental design probl
|
||||
|
||||
##### Alternatives
|
||||
|
||||
Casts are widely (mis) used. Modern C++ has constructs that eliminate the need for casts in many contexts, such as
|
||||
Casts are widely (mis) used. Modern C++ has rules and constructs that eliminate the need for casts in many contexts, such as
|
||||
|
||||
* Use templates
|
||||
* Use `std::variant`
|
||||
@@ -11629,9 +11629,10 @@ Casts are widely (mis) used. Modern C++ has constructs that eliminate the need f
|
||||
##### Enforcement
|
||||
|
||||
* Force the elimination of C-style casts
|
||||
* Warn if there are many functional style casts (there is an obvious problem in quantifying 'many').
|
||||
* Warn if there are many functional style casts (there is an obvious problem in quantifying 'many')
|
||||
* The [type profile](#Pro-type-reinterpretcast) bans `reinterpret_cast`.
|
||||
* Warn against [unnecessary pointer casts](#Pro-type-unnecessarycast).
|
||||
* Warn against [identity casts](#Pro-type-identitycast) between pointer types, where the source and target types are the same (#Pro-type-identitycast)
|
||||
* Warn if a pointer cast could be [implicit](#Pro-type-implicitpointercast)
|
||||
|
||||
### <a name="Res-casts-named"></a>ES.49: If you must use a cast, use a named cast
|
||||
|
||||
@@ -19401,8 +19402,9 @@ Type safety profile summary:
|
||||
|
||||
* <a name="Pro-type-avoidcasts"></a>Type.1: [Avoid casts](#Res-casts):
|
||||
<a name="Pro-type-reinterpretcast">a. </a>Don't use `reinterpret_cast`; A strict version of [Avoid casts](#Res-casts) and [prefer named casts](#Res-casts-named).
|
||||
<a name="Pro-type-arithmeticcast">b. </a>Don't a `static_cast` for arithmetic types; A strict version of [Avoid casts](#Res-casts) and [prefer named casts](#Res-casts-named).
|
||||
<a name="Pro-type-unnecessarycast">c. </a>Don't use pointer unnecessary pointer casts; A strict version of [Avoid casts](#Res-casts)
|
||||
<a name="Pro-type-arithmeticcast">b. </a>Don't use `static_cast` for arithmetic types; A strict version of [Avoid casts](#Res-casts) and [prefer named casts](#Res-casts-named).
|
||||
<a name="Pro-type-identitycast">c. </a>Don't cast between pointer types where the source type and the target type are the same; A strict version of [Avoid casts](#Res-casts).
|
||||
<a name="Pro-type-implicitpointercast">d. </a>Don't cast between pointer types when the conversion could be implicit; A strict version of [Avoid casts](#Res-casts).
|
||||
* <a name="Pro-type-downcast"></a>Type.2: Don't use `static_cast` to downcast:
|
||||
[Use `dynamic_cast` instead](#Rh-dynamic_cast).
|
||||
* <a name="Pro-type-constcast"></a>Type.3: Don't use `const_cast` to cast away `const` (i.e., at all):
|
||||
|
||||
Reference in New Issue
Block a user