1

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();
}  

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

but i have never seen someone suggest this solution whenever addressing this problems , what do you think ?
@KenZa Usually an accent is made on excluding the using directive.
i don't think i know what that is but it seems to me that the way i mentioned in the question is better , do you agree ?
@KenZa It is better either not to use the using directive or to use it in a minimal scope.
@KenZa Because in general there can be name collisions for numerous entities. introduced by a using directive.
|
2

The main purpose of using using namespace whatever; is to avoid typing the name of that namespace (like std and sfd) every time you want to have access to one of its members (for reasons such as saving time and also making the code look a bit cleaner). There is no problem with your solution though. It works.

But again, why would you want to use using namespace std; at the top of your source file if you're eventually going to add std:: to whichever function that needs it?

You can also write using namespace std; in a (function, loop, etc) scope so that it doesn't pollute the whole namespace of that particular source file.

1 Comment

i will only add if the function is common to both of the namespaces i declared otherwise i wouldn't

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.