mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Merge pull request #927 from ZachHenkel/master
Additional guidance around casts:
This commit is contained in:
commit
5e2f9064b2
|
@ -11653,7 +11653,8 @@ Surprised? I'm just glad I didn't crash the program.
|
||||||
|
|
||||||
##### Note
|
##### Note
|
||||||
|
|
||||||
Programmer who write casts typically assumes that they know what they are doing.
|
Programmers who write casts typically assume that they know what they are doing,
|
||||||
|
or that writing a cast makes the program "easier to read".
|
||||||
In fact, they often disable the general rules for using values.
|
In fact, they often disable the general rules for using values.
|
||||||
Overload resolution and template instantiation usually pick the right function if there is a right function to pick.
|
Overload resolution and template instantiation usually pick the right function if there is a right function to pick.
|
||||||
If there is not, maybe there ought to be, rather than applying a local fix (cast).
|
If there is not, maybe there ought to be, rather than applying a local fix (cast).
|
||||||
|
@ -11670,18 +11671,19 @@ If you feel the need for a lot of casts, there may be a fundamental design probl
|
||||||
|
|
||||||
##### Alternatives
|
##### 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 templates
|
||||||
* Use `std::variant`
|
* Use `std::variant`
|
||||||
|
* Rely on the well defined, safe, implicit conversions between pointer types
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
* Force the elimination of C-style casts
|
* Force the elimination of C-style 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').
|
|
||||||
* The [type profile](#Pro-type-reinterpretcast) bans `reinterpret_cast`.
|
* The [type profile](#Pro-type-reinterpretcast) bans `reinterpret_cast`.
|
||||||
|
* 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
|
### <a name="Res-casts-named"></a>ES.49: If you must use a cast, use a named cast
|
||||||
|
|
||||||
|
@ -11743,6 +11745,7 @@ for example.)
|
||||||
|
|
||||||
* Flag C-style and functional casts.
|
* Flag C-style and functional casts.
|
||||||
* The [type profile](#Pro-type-reinterpretcast) bans `reinterpret_cast`.
|
* The [type profile](#Pro-type-reinterpretcast) bans `reinterpret_cast`.
|
||||||
|
* The [type profile](#Pro-type-arithmeticcast) warns when using `static_cast` between 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`
|
||||||
|
|
||||||
|
@ -19589,8 +19592,11 @@ An implementation of this profile shall recognize the following patterns in sour
|
||||||
|
|
||||||
Type safety profile summary:
|
Type safety profile summary:
|
||||||
|
|
||||||
* <a name="Pro-type-reinterpretcast"></a>Type.1: Don't use `reinterpret_cast`:
|
* <a name="Pro-type-avoidcasts"></a>Type.1: [Avoid casts](#Res-casts):
|
||||||
A strict version of [Avoid casts](#Res-casts) and [prefer named casts](#Res-casts-named).
|
<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 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:
|
* <a name="Pro-type-downcast"></a>Type.2: Don't use `static_cast` to downcast:
|
||||||
[Use `dynamic_cast` instead](#Rh-dynamic_cast).
|
[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):
|
* <a name="Pro-type-constcast"></a>Type.3: Don't use `const_cast` to cast away `const` (i.e., at all):
|
||||||
|
|
|
@ -42,6 +42,7 @@ archetypical
|
||||||
arg
|
arg
|
||||||
argh
|
argh
|
||||||
args
|
args
|
||||||
|
arithmeticcast
|
||||||
arr2
|
arr2
|
||||||
arrayindex
|
arrayindex
|
||||||
ASIC
|
ASIC
|
||||||
|
@ -215,6 +216,7 @@ HTTP
|
||||||
Hyslop
|
Hyslop
|
||||||
i2
|
i2
|
||||||
IDE
|
IDE
|
||||||
|
identitycast
|
||||||
IDEs
|
IDEs
|
||||||
IEC
|
IEC
|
||||||
ifdef
|
ifdef
|
||||||
|
@ -223,6 +225,7 @@ ifstream
|
||||||
impactful
|
impactful
|
||||||
impl
|
impl
|
||||||
Impl
|
Impl
|
||||||
|
implicitpointercast
|
||||||
incompleat
|
incompleat
|
||||||
increment1
|
increment1
|
||||||
Incrementable
|
Incrementable
|
||||||
|
|
Loading…
Reference in New Issue
Block a user