Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std:: used by experienced coders rather than using namespace std;? [duplicate]

Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?

The other day when I asked a question someone replied saying if someone asks a question, show them the right way to do it instead of using namespace std; which I thought was a bit weird, as using namespace std; is way easier, But I guess I'm failing right now as I am a 'beginner' coder and you guys know better.

So I guess my question is: Why std:: instead of using namespace std;?

Thanks.

like image 977
FuzionSki Avatar asked Mar 29 '11 07:03

FuzionSki


People also ask

Why do people use STD instead of using namespace std?

The problem with putting using namespace in the header files of your classes is that it forces anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces.

What is the purpose of std :: in C++?

std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function , the primary1 operations are copy/move, destruction, and 'invocation' with operator() -- the 'function like call operator'.

Why do people not use namespace std C++?

By using using namespace std you defy the whole idea of namespaces, by polluting top one with unnecessary entries. If you frequently use some namespaced elements (like std::cout , std::endl ), pull just them with using std::cout; using std::endl; or even in the newest version of C++ with using std::cout, std::endl; .


2 Answers

From C++ FAQ:

Should I use using namespace std in my code?

Probably not.

People don't like typing std:: over and over, and they discover that using namespace std lets the compiler see any std name, even if unqualified. The fly in that ointment is that it lets the compiler see any std name, even the ones you didn't think about. In other words, it can create name conflicts and ambiguities.

https://isocpp.org/wiki/faq/coding-standards#using-namespace-std

like image 139
Jonas Bötel Avatar answered Sep 21 '22 04:09

Jonas Bötel


Simply put, you are less likely to use the wrong types or functions by mistake, or name conflicts. Say you are using your own math library, plus std, and declare using both of them, in some arbitrary order. Now, they both define function pow. Which pow are you using when you invoke pow? I think it is worth the extra typing.

like image 43
juanchopanza Avatar answered Sep 22 '22 04:09

juanchopanza