2

I understand that, in C++, we should never use:

using namespace std;

The two possible alternatives are:

1) adding using std::cout; at the beginning of the file and just type cout whenever required

2) type std::cout every time we need to use cout

My understanding is that the second method is the best. However, is this always followed in professional environments? Is it practical to follow in highly fast paced environments? I am used to the first alternative. Is it an advantage to switch?

Note: I originally posted this in Code Review and I was told that this topic belonged here. Kindly let me know if not.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
skr
  • 914
  • 3
  • 18
  • 35
  • 3
    However high your pace gets, that extra half-second spent typing `std::` cannot possibly be significant. You usually spend considerably more time thinking than typing anyway, at least I hope so ;) – Quentin Aug 23 '17 at 14:55
  • It doesn't belong here as it is primarily opinion based and not a technical question. But you may want to read [this](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) for some mediocre opinions. – nwp Aug 23 '17 at 14:55
  • 3
    `using std::cout;` has the same issues as `using std;` – Richard Critten Aug 23 '17 at 14:55
  • 2
    You *could* add a `using std::cout` inside the 1 or 2 functions that use `cout` heavily. Most functions probably won't display anything anyway. – Bo Persson Aug 23 '17 at 15:19
  • 3
    I feel better for each `std::` that I type and see in my code, because it is one more thing where I can be certain that it does what it is supposed to do, that it has documentation in case I need it, etc... of course `cout` could be the same as `std::cout` but to realize that requires me to use my brain for something when I could have avoided it – 463035818_is_not_an_ai Aug 23 '17 at 15:20

1 Answers1

4

So I have done a little bit with C++, but I would say the problem falls under the problem of namespaces in all languages. The real problem is that if you have multiple namespaces with the same function, it makes it much more difficult to read whats going on and can cause undesired results.

For example if you have two functions with the same name in two name spaces, how will the code know which one to use? Also another problem comes when you have multiple namespaces added and call a function. How does someone reading the code know which namespace the code is coming from? Having the namespace in front of the function helps make your code more readable.

Luple
  • 481
  • 3
  • 21