mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Additional guidance around casts:
- Warn on unnecessary casts - Avoid casts on arithmetic types - Add specializations of the rules for Pro-type-reinterpretcast
This commit is contained in:
@@ -11235,6 +11235,7 @@ If you feel the need for a lot of casts, there may be a fundamental design probl
|
|||||||
* Force the elimination of C-style casts
|
* Force the elimination of C-style casts
|
||||||
* Warn against named casts
|
* Warn against named 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').
|
||||||
|
* Warn against unnecessary casts.
|
||||||
|
|
||||||
### <a name="Res-casts-named"></a>ES.49: If you must use a cast, use a named cast
|
### <a name="Res-casts-named"></a>ES.49: If you must use a cast, use a named cast
|
||||||
|
|
||||||
@@ -11287,7 +11288,8 @@ for example.)
|
|||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
Flag C-style and functional casts.
|
* Flag C-style and functional casts.
|
||||||
|
* Suggest brace initialization or gsl::narrow_cast instead of named casts on arithmetic types.
|
||||||
|
|
||||||
### <a name="Res-casts-const"></a>ES.50: Don't cast away `const`
|
### <a name="Res-casts-const"></a>ES.50: Don't cast away `const`
|
||||||
|
|
||||||
@@ -18570,7 +18572,9 @@ Use of these casts can violate type safety and cause the program to access a var
|
|||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
Issue a diagnostic for any use of `reinterpret_cast`. To fix: Consider using a `variant` instead.
|
* Issue a diagnostic for any use of `reinterpret_cast`. To fix: Consider using a `variant` instead.
|
||||||
|
* Issue a separate diagnostic for any reinterpret_cast to void*. To fix: Remove the cast and allow the implicit type conversion.
|
||||||
|
* Issue a separate diagnostic for any reinterpret_cast from void*. To fix: Use static_cast instead
|
||||||
|
|
||||||
### <a name="Pro-type-downcast"></a>Type.2: Don't use `static_cast` downcasts. Use `dynamic_cast` instead.
|
### <a name="Pro-type-downcast"></a>Type.2: Don't use `static_cast` downcasts. Use `dynamic_cast` instead.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user