Search code examples
c++namespacesstdusing-directives

using namespace in c++


I have come to understand why using namespace std; is considered bad practice in c++ but let's consider for example 2 ( hypothetical ) libraries "std" and "sfd" , both of them contain a function "run()".
would the following be okay or is it still a problem :
( if i want to call "run()" from "std" )

   using namespace std;  
   using namespace sfd;  
   int main(){
       std::run();
}  

( if i want to call "run()" from "sfd" )

   using namespace std;
   using namespace sfd;
   int main(){
   sfd::run();
}  

Solution

  • There is no problem because you are using qualified names in the function calls.

    A program would be ill-formed if you used the unqualified function name in its call like

    run();
    

    In this case there would be ambiguity.