|
1 | 1 | --- |
2 | 2 | title: C26401 |
3 | | -ms.date: 07/21/2017 |
| 3 | +ms.date: 12/14/2020 |
4 | 4 | ms.topic: "conceptual" |
5 | 5 | f1_keywords: ["C26401"] |
6 | 6 | helpviewer_keywords: ["C26401"] |
7 | 7 | ms.assetid: b9d3d398-697a-4a5d-8bfe-9c667dffb90b |
8 | | -description: CppCoreCheck rule that enforces C++ Core Guidelines I.11 |
| 8 | +description: CppCoreCheck rule C26401 enforces C++ Core Guidelines I.11 |
9 | 9 | --- |
10 | 10 | # C26401 DONT_DELETE_NON_OWNER |
11 | 11 |
|
12 | | -This check detects places where moving to `owner<T>` can be a good option for the first stage of refactoring. Like C26400 it enforces rules I.11 and R.3, but focuses on the "release" portion of the pointer lifetime. It warns on any call to operator **`delete`** if its target is neither an `owner<T>` nor an implicitly assumed owner. For more information, see [C26400](c26400.md) regarding the **`auto`** declarations. This does include expressions that refer to global variables, formals, and so on. |
| 12 | +This check detects places where moving to `owner<T>` can be a good option for the first stage of refactoring. Like C26400, it enforces rules I.11 and R.3, but focuses on the "release" portion of the pointer lifetime. It warns on any call to operator **`delete`** if its target isn't an `owner<T>` or an implicitly assumed owner. For more information about **`auto`** declarations, see [C26400](c26400.md). This check includes expressions that refer to global variables, formal parameters, and so on. |
13 | 13 |
|
14 | | -Warnings C26400 and C26401 always occur with [C26409](c26409.md), but they are more appropriate for scenarios where immediate migration to smart pointers is not feasible. In such cases the `owner<T>` concept can be adopted first and C26409 may be temporarily suppressed. |
| 14 | +Warnings C26400 and C26401 always occur with [C26409](c26409.md), but they're more appropriate for scenarios where immediate migration to smart pointers isn't feasible. In such cases, the `owner<T>` concept can be adopted first, and C26409 may be temporarily suppressed. |
15 | 15 |
|
16 | 16 | ## See also |
17 | 17 |
|
18 | 18 | [C++ Core Guidelines I.11](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i11-never-transfer-ownership-by-a-raw-pointer-t-or-reference-t) |
19 | 19 |
|
20 | | -## Example |
| 20 | +## Examples |
21 | 21 |
|
22 | 22 | ```cpp |
23 | 23 | struct myStruct {}; |
@@ -46,23 +46,24 @@ void function() |
46 | 46 | } |
47 | 47 | ``` |
48 | 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. |
| 49 | +There's a C++ idiom, `delete this`, that triggers this warning. The warning is intentional, because the C++ Core Guidelines discourage this pattern. You can suppress the warning by using the `gsl::suppress` attribute, as shown in this example: |
50 | 50 |
|
51 | 51 | ```cpp |
52 | | - class MyReferenceCountingObject final |
| 52 | +class MyReferenceCountingObject final |
| 53 | +{ |
| 54 | +public: |
| 55 | + void AddRef(); |
| 56 | + void Release() noexcept |
53 | 57 | { |
54 | | - public: |
55 | | - void AddRef(); |
56 | | - void Release() noexcept |
| 58 | + ref_count_--; |
| 59 | + if (ref_count_ == 0) |
57 | 60 | { |
58 | | - ref_count_--; |
59 | | - if (ref_count_ == 0) |
60 | | - { |
61 | | - [[gsl::suppress(i.11)]] |
62 | | - delete this; |
63 | | - } |
| 61 | + [[gsl::suppress(i.11)]] |
| 62 | + delete this; |
64 | 63 | } |
65 | | - private: |
66 | | - unsigned int ref_count_{1}; |
67 | | - }; |
| 64 | + } |
| 65 | +private: |
| 66 | + unsigned int ref_count_{1}; |
| 67 | +}; |
68 | 68 | ``` |
| 69 | + |
0 commit comments