Bewerken

Delen via


Warning C26444

Don't try to declare a local variable with no name (es.84).

C++ Core Guidelines

ES.84: Don't (try to) declare a local variable with no name

An unnamed variable declaration creates a temporary object that is discarded at the end of the statement. Such temporary objects with nontrivial behavior may point to either inefficient code that allocates and immediately throws away resources or to the code that unintentionally ignores nonprimitive data. Sometimes it may also indicate plainly wrong declaration.

Remarks

  • This rule detects types with a hand-written destructor or a compiler-generated destructor that transitively calls a hand-written destructor.
  • This rule can flag code that invokes a nontrivial constructor of an RAII type.
  • 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.

Code analysis name: NO_UNNAMED_RAII_OBJECTS

Examples

struct A { A(int i); ~A(); };
void Foo()
{
    A{42}; // warning C26444: Don't try to declare a local variable with no name (es.84).
}

To fix the issue, convert the temporary object to a local.

struct A { A(int i); ~A(); };
void Foo()
{
    A guard{42}; // OK.
}

See also

C26441
ES.84: Don't (try to) declare a local variable with no name