1

From cppreference :

From the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from namespace-name is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and namespace-name.

The "inner-most" scope that contains namespace-name is the scope in which namespace-name is declared, and it must enclose the scope containing the using-directive for the using-directive to be valid.

This implies that "the nearest enclosing namespace which contains both the using-directive and namespace-name" is just the namespace in which namespace-name has been declared. In that case, why phrase it in such a verbose manner?

Am I misunderstanding something? Are there any subtleties here that I'm missing?

5
  • "in which namespace-name has been defined" I suppose you mean "declared" not "defined" Commented Jun 29, 2024 at 10:34
  • The same (enclosing) namespace can be opened and closed more than once in the same translation unit. It's being specific about which one the using applies to. Commented Jun 29, 2024 at 10:41
  • @RichardCritten using applies to the entire merged namespace in that case, doesn't it? Commented Jun 29, 2024 at 10:51
  • Your title talks about using-declarations but your question about using-directives. They are two distinct language constructs and have completely different effects on name lookup. A using-directive is of the form using namespace X;. A using-declaration is of the form using X::Y;. (To make it more confusing both using-directives and using-declarations are declarations.) Commented Jun 29, 2024 at 16:02
  • @user17732522 Fixed it, my bad. I got the tag right but messed up the title. Commented Jun 29, 2024 at 16:50

1 Answer 1

2

is just the namespace in which namespace-name has been declared. In that case, why phrase it in such a verbose manner?

That is not always the case. To make it clear, consider the following contrived example. Here, the nearest enclosing namespace that contains both the using-directive and the namespace-name is the global namespace. This means that int k = i; is ambiguous because there are two i that can be used.

If on the other hand, if we were to follow your modified rule(just the namespace in which namespace-name has been declared), then the concerning namespace would be only outer that only contains the namespace-name.

int i = 12; namespace outer { namespace n1 { int i = 10; } } namespace n2 { void f() { using namespace outer::n1; int k = i; //this is ambiguous because i already exists in global namespace } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I was wrong in assuming that the scope in which namespace-name is declared must have the using-directive.
@RajdeepSindhu Yes, exactly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.