Justification for Using Namespace Behavior

Quote from the standard:

The using directive indicates that names in the nominated namespace can be used in the area in which the use directive appears after the use directive. In an unqualified name lookup (3.4.1), the names are displayed as if they were declared in the nearest spanning namespace that contains both the usage directive and the nominated namespace.

Take a look at this code:

namespace A {

    int fn() { return 1; }

}

namespace Inner {

    int fn() { return 2; }

    namespace B {

        using namespace A;

        int z = fn();

    }

}

, , , z 1, using namespace A, , A::fn() . , z 2, Inner::fn() - , .

: " , , "?

, using namespace ?

: this - , .

+4
1

, API. , , .

++ API:

int foo(long x) { return 1; }

int main()
{
    foo(0);
}

int foo(int x) { return 2; }, .

, ++ namespace, , API , . :

namespace A {
    //no fn here, yet    
}

namespace Inner {

    int fn() { return 2; }

    namespace B {

        using namespace A;
        int z = fn();
    }
}

z 2. A fn .

:

namespace A {
    int fn() { return 1; }
}

namespace Inner {

    // no fn here

    namespace B {

        using namespace A;
        int z = fn();
    }
}

z 1. , fn Inner, , Inner API: , Inner , A::fn ( !), , .


++ 98:

#include <iostream>

namespace A {
int move = 0;
void foo()
{
    using namespace std;
    cout << move << endl;
    return 0;
}
}

int main()
{
    A::foo();
    return 0;
}

, ++ 11, using. using namespace std , std::move A::move.

+2

Source: https://habr.com/ru/post/1680739/


All Articles