Skip to content

Commit 6477059

Browse files
author
Colin Robertson
authored
Update c26474.md
1 parent 58fdd81 commit 6477059

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

docs/code-quality/c26474.md

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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

@@ -14,25 +13,29 @@ ms.assetid: 1e23a8e6-97fa-47f5-a279-b52aa2efafa4
1413
**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 a derived type to its base when invoking a base member function that is not hidden by the derived type.
20+
The rule ID should be interpreted as "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 should not 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

2829
## 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,35 +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
5154
52-
```cpp
53-
// ...
54-
template<class T>
55-
bool register_buffer(T *buffer) {
56-
auto p = buffer;
57-
return buffers_.insert(p).second;
58-
}
59-
// ...
60-
```
55+
This example demonstrates using casts to access base-class member functions:
6156
62-
## Example 2
63-
Using casts to access base-class member functions
6457
```cpp
6558
struct struct_1
6659
{
67-
void foo();
68-
void bar();
60+
void foo();
61+
void bar();
6962
};
63+
7064
struct struct_2 : struct_1
7165
{
72-
void foo(); // this definition hides struct_1::foo
66+
void foo(); // this definition hides struct_1::foo
7367
};
7468
7569
void fn(struct_2* ps2)
7670
{
77-
static_cast<struct_1*>(ps2)->foo(); // this cast is necessary to access `struct_1::foo`
78-
// alternatively ps2->struct_1::foo();
79-
static_cast<struct_1*>(ps2)->bar(); // this cast is unnecessary and can be done implicitly
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
8074
}
8175
```

0 commit comments

Comments
 (0)