|
1 | 1 | --- |
2 | | -description: "Learn more about: Warning C26444 NO_UNNAMED_RAII_OBJECTS" |
3 | 2 | title: Warning C26444 |
4 | | -ms.date: 01/18/2017 |
| 3 | +description: "Learn more about: Warning C26444 NO_UNNAMED_RAII_OBJECTS" |
| 4 | +ms.date: 05/11/2023 |
5 | 5 | f1_keywords: ["C26444", "NO_UNNAMED_RAII_OBJECTS"] |
6 | 6 | helpviewer_keywords: ["C26444"] |
7 | 7 | --- |
8 | 8 | # Warning C26444 |
9 | 9 |
|
10 | | -> Avoid unnamed objects with custom construction and destruction. |
| 10 | +> Don't try to declare a local variable with no name (es.84). |
11 | 11 |
|
12 | 12 | ## C++ Core Guidelines |
13 | 13 |
|
14 | | -[ES.84: Don't (try to) declare a local variable with no name](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-noname) |
| 14 | +[ES.84: Don't (try to) declare a local variable with no name](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-noname) |
15 | 15 |
|
16 | 16 | Unnamed (that is, temporary) objects with non-trivial behavior may point to either (a) inefficient code that allocates and immediately throws away resources or (b) to the code that unintentionally ignores non-primitive data. Sometimes it may also indicate plainly wrong declaration. |
17 | 17 |
|
18 | | -## Notes |
| 18 | +## Remarks |
19 | 19 |
|
20 | | -- This rule detects types with non-deleted destructors. Keep in mind that destructors can be compiler generated. |
21 | | -- The warning can flag code that isn't compiler generated and that invokes either a non-default constructor of a RAII type or a function that returns an object of such type. This warning helps to detect ignored call results in addition to wrong declarations. |
| 20 | +- This rule detects types with non-trivial destructors. Keep in mind that destructors can be compiler generated. |
| 21 | +- The warning can flag code that invokes either a non-trivial constructor of a RAII type. |
22 | 22 | - The logic skips temporaries if they're used in higher-level expressions. One example is temporaries that are passed as arguments or used to invoke a function. |
23 | | -- The standard library implementation may have different versions of destruction logic for some types (for example, containers). This can produce noisy warnings on debug builds because it's customary to ignore iterators returned from calls like [`std::vector::insert`](../standard-library/vector-class.md#insert). While such warnings aren't actionable in most cases, they're legitimate in pointing to the place where some non-obvious work is done in temporary objects. |
24 | 23 |
|
25 | 24 | Code analysis name: `NO_UNNAMED_RAII_OBJECTS` |
26 | 25 |
|
27 | | -## Example: Ignored call result |
| 26 | +## Examples |
28 | 27 |
|
29 | 28 | ```cpp |
30 | | -std::string ToTraceMessage(State &state); |
31 | | -void SaveState(State &state) |
| 29 | +struct A { A(int i); ~A(); }; |
| 30 | +void Foo() |
32 | 31 | { |
33 | | - // ... |
34 | | - ToTraceMessage(state); // C26444, should we do something with the result of this call? |
| 32 | + A{42}; // warning C26444: Don't try to declare a local variable with no name (es.84). |
35 | 33 | } |
| 34 | +``` |
36 | 35 |
|
37 | | -Example: Ignored call result - fixed. |
38 | | -std::cerr << ToTraceMessage(state); |
| 36 | +To fix the issue, convert the temporary object to a local. |
39 | 37 |
|
40 | | -Example: Unexpected lifetime. |
41 | | -void SplitCache() |
| 38 | +```cpp |
| 39 | +struct A { A(int i); ~A(); }; |
| 40 | +void Foo() |
42 | 41 | { |
43 | | - gsl::finally([] { RestoreCache(); }); // C26444, RestoreCache is invoked immediately! |
44 | | - //... |
| 42 | + A guard{42}; // OK. |
45 | 43 | } |
46 | | - |
47 | | -Example: Unexpected lifetime - fixed. |
48 | | -const auto _ = gsl::finally([] { RestoreCache(); }); |
49 | 44 | ``` |
50 | 45 |
|
51 | 46 | ## See also |
52 | 47 |
|
53 | | -[ES.84: Don't (try to) declare a local variable with no name](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md) |
| 48 | +[C26441](C26441.md)\ |
| 49 | +[ES.84: Don't (try to) declare a local variable with no name](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-noname) |
0 commit comments