gpt4 book ai didi

c++ - `using namespace std::placeholders;` 是否不符合标准?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:29 25 4
gpt4 key购买 nike

来自 § 2.10.3.2:

Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

考虑到这一点,这个程序是否符合标准?因为它使 _1 等可从全局命名空间获得?还是可以因为 std::placeholders 被认为是“实现”?还是因为 _1 实际上不在全局命名空间中?还有别的吗?

using namespace std::placeholders;
int main(){}

最佳答案

我觉得这个程序很好。严格来说,using 指令不会将任何名称放入命名空间 - 它使名称可以通过名称查找访问,但实际上并没有使它们成为包含 using 指令的命名空间的成员。

引用 C++11, 7.3.4:

2 A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. —end note ]

3 A using-directive does not add any members to the declarative region in which it appears. ...

请注意“可以使用”、“就好像它们已被声明”等语言。没有提及实际使名称成为不同 namespace 的成员。

关于通过限定名称访问,3.4.3.2( namespace 的限定名称查找)说:

2 For a namespace X and name m, the namespace-qualified lookup set S(X,m) is defined as follows: Let S'(X,m) be the set of all declarations of m in X and the inline namespace set of X (7.3.1). If S'(X,m) is not empty, S(X,m) is S'(X,m); otherwise, S(X,m) is the union of S(Ni,m) for all namespaces Ni nominated by using-directives in X and its inline namespace set.

即,使用指令也有单独的规则。

由此我可以得出结论,using 指令不会使任何名称成为命名空间的成员,因此不会触发 _ 全局命名空间规则,也不会发生未定义的行为。

关于c++ - `using namespace std::placeholders;` 是否不符合标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27903704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com