Hacker News new | ask | show | jobs
by mpixel 999 days ago
I'm not a C++ programmer, could you explain why this is a bad idea?
2 comments

If it's in a header file it imports the namespace into everything else. People make a big deal about it even though most of the time it isn't. If it is isolated to a compilation unit it's fine.
> People make a big deal about it even though most of the time it isn't.

For your personal project maybe it isn't. On shared or reusable code it's quite problematic.

It's problematic if it is in a header file that is going to be reused, not if it is in its own compilation unit.
It's less problematic in that case, but still problematic. It's less apparent during initial authorship, because you often have all the names that might collide already in your head at that point. It's during later maintenance when you lack context (like when you/your coworkers haven't seen, or don't recall, the code) that the problems become more apparent. You're basically digging holes behind you as you're walking along, and even though you might not trip over them, someone else walking on that path eventually will.
Not really. If someone is using vector<type> or unordered_map it's unlikely its anything else and if you are using an IDE, you can check easily. You are basically implying that anything short of full names are a problem which is just not true.
> Not really. If someone is using vector<type> or unordered_map it's unlikely its anything else

There are far less distinctive names than those in namespace std. Have you seen remove(), erase(), move(), search(), apply(), hash, format(), etc.?

Not to mention std::operator overloads like == and < that you suddenly drag into overload resolution needlessly, which can bring their own fun into the mix.

> if you are using an IDE, you can check easily

Lots of people don't. And even those who do, don't immediately have the symbol available to go to its definition. It's quite normal to have to wait O(minutes) for semantic analysis to become ready.

> You are basically implying that anything short of full names are a problem which is just not true.

No, very much not so. If you use it on something that's unlikely to collide (like std::cerr), `using std::foo;` is generally fine outside of headers. `using namespace std;` is the one that's problematic.

How else would you know that “cout” is std::cout and not some other cout? /s
Are you sure every name in std is as unlikely to collide as cout? What about format(), apply(), ...?
If a name collides, nothing prevents you from explicitly qualifying it.
When it silently changes the behavior you have no idea there was a collision.