Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not define unnamed local variables in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report