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.
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.
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'.
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; .
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 thatusing namespace std
lets the compiler see anystd
name, even if unqualified. The fly in that ointment is that it lets the compiler see anystd
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With