Scope of the c++ using directive

Posted by ThomasMcLeod on Stack Overflow See other posts from Stack Overflow or by ThomasMcLeod
Published on 2012-09-20T16:01:49Z Indexed on 2012/09/21 21:39 UTC
Read the original article Hit count: 196

From section 7.3.4.2 of the c++11 standard:

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 ]

What do the second and third sentences mean exactly? Please give example.

Here is the code I am attempting to understand:

namespace A
{
    int i = 7;
}
namespace B
{
    using namespace A;
    int i = i + 11;
}
int main(int argc, char * argv[])
{
    std::cout << A::i << " " << B::i << std::endl;
    return 0;
}

It print "7 7" and not "7 18" as I would expect.

Sorry for the typo, the program actually prints "7 11".

© Stack Overflow or respective owner

Related posts about c++

Related posts about namespaces