2

It's obvious why using

using namespace std;

is considered bad practice. As I'm fairly new to C++ (about 8 months now) I wondered if it is still considered bad practice when I use

using std::cout;
using std::cin;
using std::endl;

and so on to only include what I need instead of including the whole namespace. My old teacher always told us to not use that as well but my new teacher told us it's just fine to use the using-Declaration compared to including the whole namespace.

What do you use and would you consider it a bad practice or not?

I hope this question is not a duplicate but I did not find another question like this, just questions about why it's considered to be bad practice to use using namespace std;, but like mentioned above I already know that.

user18596
  • 129
  • 2
  • Look at the reasons why `using namespace std` is bad. Then see if they apply to `using std::cout` You should be able to answer your question nicely that way – UKMonkey Jun 08 '18 at 09:06
  • 4
    Yes, all are bad. Type out `std::`; it avoids namespace clashes and the potential for unwanted variable shadowing. It that bores you and you have a Windows keyboard, then get some use out of that silly windows button and set it to output `std::`. – Bathsheba Jun 08 '18 at 09:08
  • namespaces are something extremely useful, by `using` a namespace or parts of it you are bypassing the feature namespaces are good for – 463035818_is_not_an_ai Jun 08 '18 at 09:16
  • Yes, it's bad practice. Don't do it. – Ron Jun 08 '18 at 09:17
  • 2
    acutally i dont understand why so many want to get rid of the `std::` in front of their `std` stuff. For me every `std::` in front of something means a green flag indicating lots of less debugging and testing. – 463035818_is_not_an_ai Jun 08 '18 at 09:21
  • @user463035818 : It's due to curiosity and unawareness which wouldn't be there if C++ was learnt with the help of a good book. Happens with almost everyone trying to learn C++. – Vishaal Shankar Jun 08 '18 at 09:39
  • @VishaalShankar yes, even otherwise good books are `using namespace std;` for the sake of brevity and make ppl believe that it is something nice to have – 463035818_is_not_an_ai Jun 08 '18 at 09:44
  • Opinions on this (and yes, they will primarily be opinions) usually depend on the *scope* of the using. Within a single function, it's a useful construct that may help keep code within reasonable line lengths, but at file scope, the downsides grow larger. And there are cases where we *must* import names from `std` or elsewhere (for ADL to work) - most of use prefer to do so in the minimum possible scope. – Toby Speight Jun 26 '18 at 14:22

1 Answers1

1

I didn't like my original answer, too wordy, so here's a better rewrite.
Here's a good answer on why using namespace is bad.

With that answer in mind, if the library Foo has function kept and Bar has the function lept. With 2 using-directives, you can use both kept and lept without the scope. If Foo decides to update, and adds a function lept, then your compiler of choice will either give an error, or have undefined behavior*. If you use using Foo::kept and using Bar::lept, then an update on Foo that adds lept will not cause any error, and you can use Foo::lept to call that new function. It is also considered proper form with using-directives to not use them globally. Rather, for each function that you want to use a using-directive, redefine it. It is not bad practice to use using std::cout and the like, and in some instances, allows for quick library changes. For instance, I am using a JSON parsing library. To test them all, I added a using json = LIBRARY::json line to the start of my program, allowing me to quickly change out libraries if I did not like the one I had.

*For example, if Bar has lept defined as double lept(double, double), but Foo has lept defined as int lept(int, int), then a call to lept with 2 int inputs would call to Foo::lept or give an error on compile time. This means you won't know you have a massive error in your program until you test it.