You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Don't cast between pointer types when the conversion could be implicit."
13
12
14
-
**C++ Core Guidelines**:
13
+
**C++ Core Guidelines**:\
15
14
Type.1: Avoid casts
16
15
17
-
In some cases, implicit casts between pointer types can safely be done and don’t require user to write specific cast expression. This rule finds instances of such unnecessary casting which can be removed.
16
+
In some cases, implicit casts between pointer types are safe and don't require you to write a specific cast expression. This rule finds instances of unnecessary casts you can safely remove.
18
17
19
18
## Remarks
20
19
21
-
- The rule ID is a bit misleading: it should be interpreted as "implicit cast is not used where it is acceptable".
22
-
- The rule is applicable to pointers only and checks static casts and reinterpret casts.
23
-
- The following cases are acceptable pointer conversions that should not use explicit cast expressions:
24
-
- conversion to nullptr_t;
25
-
- conversion to void*;
26
-
- conversion from derived type to its base.
20
+
The rule ID should be interpreted as "An implicit cast isn't used where it's acceptable."
21
+
22
+
This rule is only applicable to pointers. It checks static casts and reinterpret casts.
23
+
24
+
These cases are acceptable pointer conversions that shouldn't use explicit cast expressions:
25
+
- conversion to `nullptr_t`;
26
+
- conversion to `void*`;
27
+
- conversion from a derived type to its base when invoking a base member function that's not hidden by the derived type.
27
28
28
-
## Example
29
+
## Example 1
29
30
30
-
unnecessary conversion hides logic error
31
+
An unnecessary conversion hides a logic error in this example:
31
32
32
33
```cpp
33
34
template<classT>
34
35
boolregister_buffer(T buffer) {
35
36
auto p = reinterpret_cast<void*>(buffer); // C26474, also 26490 NO_REINTERPRET_CAST
37
+
// To fix, declare buffer as T*, and use this to define p:
0 commit comments