Skip to content

Commit 6acffc8

Browse files
authored
Clarify behavior of C26409 for delete this idiom
1 parent 0f7adea commit 6acffc8

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

docs/code-quality/c26409.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ void f(int i)
3535
auto unique = std::make_unique<int[]>(i); // prefer using smart pointers over new and delete
3636
}
3737
```
38+
39+
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.
40+
41+
```cpp
42+
class MyReferenceCountingObject final
43+
{
44+
public:
45+
void AddRef();
46+
void Release() noexcept
47+
{
48+
ref_count_--;
49+
if (ref_count_ == 0)
50+
{
51+
[[gsl::suppress(r.11)]]
52+
delete this;
53+
}
54+
}
55+
private:
56+
unsigned int ref_count_{1};
57+
};
58+
```

0 commit comments

Comments
 (0)