Skip to content

Commit b6640e3

Browse files
authored
Clarify behavior of C26401 for delete this idiom
1 parent 0f7adea commit b6640e3

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

docs/code-quality/c26401.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,24 @@ void function()
4545
delete pMyStruct; // no warning.
4646
}
4747
```
48+
49+
There is a C++ idiom `delete this` that triggers this warning. The warning is intentional as this pattern is discouraged by the C++ Core Guidelines. The warning can be suppressed using the `gsl::suppress` attribute in such cases. See the example below.
50+
51+
```cpp
52+
class MyReferenceCountingObject final
53+
{
54+
public:
55+
void AddRef();
56+
void Release() noexcept
57+
{
58+
ref_count_--;
59+
if (ref_count_ == 0)
60+
{
61+
[[gsl::suppress(i.11)]]
62+
delete this;
63+
}
64+
}
65+
private:
66+
unsigned int ref_count_{1};
67+
};
68+
```

0 commit comments

Comments
 (0)