Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Why not define unnamed local variables in C++

Shulou Source: shulou.com Published: 2022-06-01 19:30:39 09月24日 Update

This article mainly explains why unnamed local variables should not be defined in C++. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "Why not define anonymous local variables in C++"!

ES.84: do not define unnamed local variables

Reason (reason)

There is no such thing. What looks to a human like a variable without a name is to the compiler a statement consisting of a temporary that immediately goes out of scope.

There is no reason to do so. From a human point of view, it is an unnamed variable, but from the compiler it looks like a statement made up of temporary variables that will soon leave scope.

Example, bad (negative example)

Void f ()

{

Lock {mx}; / / Bad

/ /...

}

This declares an unnamed lock object that immediately goes out of scope at the point of the semicolon. This is not an uncommon mistake. In particular, this particular example can lead to hard-to find race conditions.

The code defines an unnamed lock that immediately leaves the scope bounded by semicolons. This is not a particular mistake. Often, this common example leads to a competitive condition that is difficult to find.

Note (Note)

Unnamed function arguments are fine.

There is no problem with nameless function arguments.

Enforcement (implementation recommendations)

Flag statements that are just a temporary.

Tags are only temporary statements.

At this point, I believe you have a deeper understanding of "why not define an anonymous local variable in C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Variable local clocked action content statement right away learning practical deeper this is not code example interest function semicolon reason parameter negative just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Huawei Xiaomi NVidia