“using-declarations” 的正确用法是什么?

huangapple go评论59阅读模式
英文:

What is the right use of "using-declarations"?

问题

以下是您要翻译的内容:

原始代码:

using namespace std::placeholders;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

代码运行正常,但 cpplint 报告说我应该使用 "using-declarations",然后我将代码更改为:

using _1 = std::placeholders::_1;
using _2 = std::placeholders::_2;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

但现在编译器报错:

error: _1 in namespace std::placeholders does not name a type

应该使用正确的 using-declarations 吗?

更新
到目前为止的回答建议我重新添加这行:

using namespace std::placeholders;

正如我所描述的,cpplint 不喜欢使用命名空间,这是 cpplint 的消息:

Do not use namespace using-directives. Use using-declarations instead.

您能建议如何处理这个问题吗?

英文:

I was using this first -

using namespace std::placeholders;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

The code worked fine, but cpplint complained and said I should use "using-declarations", I then changed the code to:

using _1 = std::placeholders::_1;
using _2 = std::placeholders::_2;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

But now compiler complains:

error: ‘_1’ in namespace ‘std::placeholders’ does not name a type

What should be the right using-declarations?

Update
The replies so far suggest me to add back this line:
using namespace std::placeholders;

As I described, cpplint is not happy with using namespace, here is cpplint message:

Do not use namespace using-directives.  Use using-declarations instead.

Could you suggest how to do about it?

答案1

得分: 1

std::placeholders::_1 不是一种类型,而是一个未指定类型的对象,因此不能对其应用using指令。只需将namespace std::placeholders引入所需的作用域即可。

using namespace std::placeholders;
my_cb = std::bind(&Myclass::handler, this, _1, _2);
英文:

std::placeholders::_1 is not a type, it's an object of an unspecified type, thus using directive is not applicable to it. Just bring the namespace std::placeholders into a required scope

using namespace std::placeholders;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

答案2

得分: 1

以下是翻译好的部分:

"The message wants you to write" -> "这条消息要求你编写"
"using std::placeholders::_1;" -> "使用 std::placeholders::1;"
"using std::placeholders::2;" -> "使用 std::placeholders::2;"
"/.../" -> "/.../"
"etc. This is what using declarations are." -> "等等。这就是using声明的作用。"
"What you tried with syntax of the form using /*...*/ = /*...*/; are type aliases," -> "你尝试的形式为using /*...*/ = /*...*/;是_类型别名
,"
"a third meaning of using distinct from both using directives (using namespace /*...*/; and using declarations (using /*...*/;)."
-> "这是using的第三种含义,与_使用指令
using namespace /*...*/;)和_使用声明
using /*...*/;)都不同。"
"However, I don't think that is technically allowed at global namespace scope because names starting with an underscore are reserved there." -> "但是,我认为从技术上讲,在全局命名空间范围内是不允许的,因为以下划线开头的名称在那里是保留的。"
"Do it in a narrower scope." -> "在更窄的范围内执行它。"
"The message is from clang-tidy and implements a particular rule in Google's style guides that flat out forbids any use of using namespace." -> "这条消息来自clang-tidy,并实施了Google风格指南中明令禁止使用using namespace的特定规则。"
"However, the std::placeholders namespace is specifically designed to be used in this way and avoids making any other names visible." -> "然而,std::placeholders命名空间是专门设计用于这种方式,并避免使其他名称可见。"
"As long as it is used in a narrow scope (not at the global scope) I would consider it fine." -> "只要它在较窄的范围内使用(而不是在全局范围内),我认为它是可以接受的。"
"Opinions on this rule will differ." -> "关于这个规则的意见会有不同。"

英文:

The message wants you to write

using std::placeholders::_1;
using std::placeholders::_2;
/*...*/

etc. This is what using declarations are. What you tried with syntax of the form using /*...*/ = /*...*/; are type aliases, a third meaning of using distinct from both using directives (using namespace /*...*/; and using declarations (using /*...*/;).

However, I don't think that is technically allowed at global namespace scope because names starting with an underscore are reserved there. Do it in a narrower scope.

The message is from clang-tidy and implements a particular rule in Google's style guides that flat out forbids any use of using namespace. However, the std::placeholders namespace is specifically designed to be used in this way and avoids making any other names visible. As long as it is used in a narrow scope (not at the global scope) I would consider it fine. Opinions on this rule will differ.

huangapple
  • 本文由 发表于 2023年2月18日 14:52:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定