首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.84:不要定义无名局部变量

C++核心准则ES.84:不要定义无名局部变量

作者头像
面向对象思考
发布2020-06-09 15:08:43
5490
发布2020-06-09 15:08:43
举报

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

ES.84:不要定义无名局部变量

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.

不存在这么做的理由。以人的角度来看是一个无名变量,但从编译器看起来就是一个马上会离开作用域的临时变量构成的语句。

Example, bad(反面示例)

void f()
{
    lock<mutex>{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.

代码中定义了一个马上会离开以分号为界的作用域的无名锁。这不是一个特别的错误。通常,这个常见的例子会导致一个难以发现的竞争条件。

Note(注意)

Unnamed function arguments are fine.

无名的函数参数是没有问题的。

Enforcement(实施建议)

Flag statements that are just a temporary.

标记只是临时存在的语句。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es84-dont-try-to-declare-a-local-variable-with-no-name

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档