Skip to content

Commit 3a42316

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#3045 from MicrosoftDocs/FromPublicMasterBranch
Confirm merge from FromPublicMasterBranch to master to sync with https://github.com/MicrosoftDocs/cpp-docs (branch master)
2 parents 8c767c1 + 330d1eb commit 3a42316

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

docs/code-quality/c26474.md

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
---
22
title: C26474
3-
ms.date: 11/15/2017
3+
ms.date: 08/11/2020
44
ms.topic: "conceptual"
55
f1_keywords: ["C26474"]
66
helpviewer_keywords: ["C26474"]
77
ms.assetid: 1e23a8e6-97fa-47f5-a279-b52aa2efafa4
8-
98
---
109
# C26474 NO_IMPLICIT_CAST
1110

1211
"Don't cast between pointer types when the conversion could be implicit."
1312

14-
**C++ Core Guidelines**:
13+
**C++ Core Guidelines**:\
1514
Type.1: Avoid casts
1615

17-
In some cases, implicit casts between pointer types can safely be done and dont 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.
1817

1918
## Remarks
2019

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.
2728

28-
## Example
29+
## Example 1
2930

30-
unnecessary conversion hides logic error
31+
An unnecessary conversion hides a logic error in this example:
3132

3233
```cpp
3334
template<class T>
3435
bool register_buffer(T buffer) {
3536
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:
38+
// auto p = buffer;
3639
return buffers_.insert(p).second;
3740
}
3841

@@ -47,14 +50,26 @@ void merge_bytes(std::uint8_t *left, std::uint8_t *right)
4750
}
4851
```
4952
50-
unnecessary conversion hides logic error - reworked
53+
## Example 2
54+
55+
This example demonstrates using casts to access base-class member functions:
5156
5257
```cpp
53-
// ...
54-
template<class T>
55-
bool register_buffer(T *buffer) {
56-
auto p = buffer;
57-
return buffers_.insert(p).second;
58+
struct struct_1
59+
{
60+
void foo();
61+
void bar();
62+
};
63+
64+
struct struct_2 : struct_1
65+
{
66+
void foo(); // this definition hides struct_1::foo
67+
};
68+
69+
void fn(struct_2* ps2)
70+
{
71+
static_cast<struct_1*>(ps2)->foo(); // This cast is necessary to access struct_1::foo
72+
// Alternatively, use ps2->struct_1::foo();
73+
static_cast<struct_1*>(ps2)->bar(); // This cast is unnecessary and can be done implicitly
5874
}
59-
// ...
6075
```

0 commit comments

Comments
 (0)