Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `using namespace std::placeholders;` non-conformant?

From § 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.

With that in mind, is this program standard conformant? As it makes _1 etc available from the global namespace? Or is it OK because std::placeholders is considered "implementation"? Or is it OK because _1 isn't actually in the global namespace? Something else?

using namespace std::placeholders;
int main(){}
like image 738
OmnipotentEntity Avatar asked Mar 18 '23 09:03

OmnipotentEntity


1 Answers

I think the program is fine. Strictly speaking, a using-directive does not put any names into a namespace - it makes the names reachable by name lookup, but does not actually make them members of the namespace enclosing the using-directive.

Quoting 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. ...

Notice the language "can be used," "as if they were declared" etc. No mention of actually making a name member of a different namespace.

As for access through a qualified name, 3.4.3.2 (Qualified name lookup for namespaces) says:

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.

I.e., separate rules for using-directives as well.

I would conclude from this that a using-directive does not make any names members of a namespace, and thus the _ global namespace rules is not triggered and no Undefined Behaviour occurs.

like image 199
Angew is no longer proud of SO Avatar answered Mar 28 '23 09:03

Angew is no longer proud of SO