using namespace std

I know it's bad practice to do using namespace std;
If this is the case, why do so many universities and beginner level courses declare this statement so many times? I know it's bad to see it in a header file and see it being declared in the global scope. Why do programming courses not tell students that it is bad to do this? Many of the videos online and even professors just tell us that it saves us from having to type std:: a lot. The first time I've heard that using the namespace standard globally was bad was from browsing these forums a few years back.
During all of my foundations of computer programming class, I've declared the namespace standard globally. Is it not a big deal to use the namespace standard globally when dealing with small C++ projects? When will using the namespace standard globally actually conflict with projects?
Ok. But wouldn't one know better to name a declare a variable/function that doesn't conflict with a name from the standard library?

1
2
// not using namespace std;
int cout = 10; // valid 


1
2
using namespace std;
int cout = 10; // not valid 
Do you know all the names in the standard library?

Lets assume that you do, and thus you write a program with unique names. Tomorrow the evil C++ standard committee will add new names to C++20 standard library just to ruin your day. Names that you did not know. Names that conflict with yours.
Thank you both!

evil C++ standard committee

Don't know why, but this made me chuckle more than I should have.

I've finally seem to have wrapped my head around the global use of namespace standard. For what reasons do universities allow such usage of it globally? Wouldn't it be better to just introduce students to something like using std::cout; instead of the global namespace? Forgive my ignorance, but is declaring a form of using globally bad in general?
fiji885 wrote:
Wouldn't it be better to just introduce students to something like using std::cout; instead of the global namespace? Forgive my ignorance, but is declaring a form of using globally bad in general?


Hi,

The trouble with that is that it's not too hard to have lots of those at the beginning of a file, so it becomes a pain. Imagine needing various containers and algorithms, I have had up to 14 using statements in the past, so I don't do that anymore. But they are not a bad thing per se.

If someone sends me code that has std:: throughout, then I don't have to do anything to it.

The other thing about specifying std:: is that it says which of something one is using: std::copy not boost::copy or some other copy from a library somewhere.

Speaking of boost, it's seems to be ok to have using namespace if the namespace is small, boost do this all the time in their code examples. I have found that to be a pain also.

Stroustrup himself also has using namespace std; , one would think if there was anyone who had an interest in doing things "correctly" , it would be him. Perhaps he feels it is OK for small pieces of student code.

Ideally one should put their own code in their own namespaces.

Regards :+)
Topic archived. No new replies allowed.