+ 9
Why is it not advisable to write "using namespace std"?
I have oftentimes heared that its not a good practice to write "using namespace std;" in a C++ program. Why is it so?
25 Answers
+ 13
Because there are a lot of names in std and by using all of them everywhere and at all times, you can lose track (in a longer code).
For example if someone reads your source, it can be troublesome to figure out where a name that is used in the code even comes from.
If you explicitly access what you need and nothing more, it's easier to know what belongs where.
You could write:
using std::cout, std::cin;
if that is all you use anyway. Then every name is explicitly 'declared'.
Or you use using namespace std (when needed) only in the context of a function, locally, where it's easy to oversee who has access to it (just the function).
+ 13
AZTECCO I'm curious to see your response as well.
I think this FAQ on isocpp.org covers all the talking points for this question as well.
https://isocpp.org/wiki/faq/coding-standards#using-namespace-std
+ 9
David Carroll Thank you so much sir for the article. Its really helpful :)
Btw I am ROFL after reading this about pointer casts in this article 😂
"I think of them as a filter on error messages: the compiler wants to complain because it sees you’re doing something stupid, but it also sees that it’s not allowed to complain due to your pointer-cast, so it drops the error message into the bit-bucket. It’s like putting duct tape on the compiler’s mouth: it’s trying to tell you something important, but you’ve intentionally shut it up."
+ 9
Nobody quoting Ace? What is this.
>:c
https://www.sololearn.com/Discuss/294730/?ref=app
+ 6
AZTECCO I have to disagree with your statement:
"... this article says exactly what I say."
You've stated multiple times that it's a bad practice to bring specific functions into the local namespace with the using ~directive~ declaration. (i.e. using std::cout)
However, the article states it's fine as long as it is known to not collide with another namespace and that it is consistent within your team.
It is probably more suitable to state it's considered a bad practice on your team. However, the specific reason is what remained missing from your explanation. In the end, it comes down to a matter of preference.
For me, personally, I prefer explicitly typing std::cout instead of cout. However, I do so, not because it's a good practice. Rather, it's my preference.
Also, it's an irrelevant argument to state that you aren't the only one who believes this. This argument suggests either a lack of deeper understanding or the understanding is, again, a matter of opinion.
+ 6
+ 5
Because you can run in trouble with namespace clashes!
If you have multiple namespaces with the same function your code doesn't know which to use.
And for readability too.
When you write small codes here is all fine but in real programming not.
TIP: you can set your keyboard to output std:: when you press one specific button.
+ 4
AZTECCO Had I known that a website called isocpp containing this information exists, there was no point in asking this question here. We learn from each other. There are tons of websites out there on the internet and one can't keep track of each and every website.
+ 4
For smaller programs, there is no harm using it. I am steering well clear of the debate on its use in larger programs.
+ 2
Well, the point of having datatypes, functions, classes and all that 'encapsulating' stuff, is handling complexity, right? Prevent name clashes, reduce the scope of error, increase maintainability.
So everything that counteracts that by opening up the scopes again, by creating side effects from functions, using global variables, keeping stuff in classes generally public and accessing it haphazardly, could be considered 'bad' in this context.
I can see by writing 'using namespace std;' right at the top of your code is problematic from that perspective.
On the other hand I don't understand, how 'using std::cout' in a local context, let's say in a method that fits in one or two screens, could be reasonably called 'bad practice' .
+ 2
😂😂😂
Infinity ♾️ What an explanation!!
+ 2
Infinity ♾️ first thing: iso cpp ,cppreference and stack overflow are really important, you will visit them all the time, after comes geeks for geeks and others.. but the first 3 are really essential .
I'm glad you've discovered them.
+ 2
I don't blame you Infinity ♾️
You learned something really useful and that's great.
+ 1
using "using std::cout" is considered a bad practice too and the same for context
+ 1
What specifically makes 'using' a bad practice when you don't abuse it to create something like a global namespace of the complete stdlib?
+ 1
Your local context example is fine, it's not to avoid like the rest, but it's considered bad practice, not just from me eh..
Why? For the sake of simplicity.. that's all..
I told you before what means abuse or something like?
I knew you exactly you knew what you were referring to..
But is too complicated.
using always xxx::yyy is simple and clear and doesn't cost much.
define when to use one style over the other is complicated, and code could be changed.
So if I give an advice is to use always std::xxx
This doesn't mean your adding is wrong.
0
Really, AZTECCO? Even if you do it non-globally?
0
Yes, it's safe but considered bad practice.
0
What specifically means 'abuse' and 'something like'? You got the answer then.