The short answer . The local specific name, and the name declared using the using declaration, hides non-local names.
Detailed response:
Your question is very interesting. I did not open the C ++ 98.03.11 standards for this question, but open the Bjarne Stroustrup book
A namespace is a named area. Ambiguity can be eliminated using two methods:
- create synonyms with NS :: x; (Using declaration)
- Create synonyms for all variables using NS :: x namespace (Using the directive)
The answer to your question is here:
Appendix B 10.1 local definitions, and names defined with using-declaration hides the name of a non-local definitions.
Bonus with the opposite situation:
Also if you
using NamespaceA::MyFunction; using NamespaceB::MyFunction;
change to
using namespace NamespaceB;
Then, due to the text below, you get a situation with calling only void MyFunction (int object)
8.2.8.2 Names explicitly declared in namespace (also made with using declaration) have priority over the names made available by using directives
Additional code for the game:
#include <iostream> // var in global namespace const char* one = "G_one"; // vars in named namespace namespace NS1 { const char* one = "NS1_one"; const char* two = "NS1_two"; const char* three = "NS1_three"; } namespace NS2 { const char* one = "NS2_one"; const char* two = "NS2_two"; const char* three = "NS2_three"; } int main(int argc, char *argv[]) { using namespace NS1; // using-directive using namespace NS2; // using-directive // const char* two = "L_two"; // local namespace using NS2::two; // using-declaration // C++ rules // Local names and names with using-declarations // takes precedence over the name of the NS std::cout << "two: " << two << std::endl; //std::cout << "three: " << three << std::endl; // ambiguous symbol // But the name in global-namespace does not have priority over imported name from namespace //std::cout << "one: " << one << std::endl; // ambiguous symbol. Because wGlobal names does not have priority over return 0; }
bruziuz
source share