Is using namespace..like bad?
Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?
Every time I use using namespace std
I always get that "thats a terrible programming habit". Now I'm graduating this December with my BS in CS but I don't claim to know everything, but no one has ever explained why this is so bad. I understand what it does but I honestly don't see a huge deal with it.
Anyone care to explain? In my mind it just makes typing cout
a whole lot more bearable than std::cout
.
I can understand why you wouldn't want to put it in a header file, but just in a normal implementation file... I dont see why it would be a problem.
found this useful post elsewhere:
Namespaces separate and organize functionality. You can have a xander333::sort()
function and it won't conflict with std::sort()
or boost::sort()
or any other sort(). Without namespaces there can be only one sort()
.
Now let's say you've put "using namespace std;" in all your source files and you've implemented a simple templated function called fill()
in the global namespace of one of your files. This file also depends on a header from libFoo -- foo.hpp
. Version 2.1 of libFoo comes out and all of a sudden your program no longer compiles. You version of fill()
suddenly conflicts with another fill()
! What happened?
It turns out that the folks implementing libFoo included in the new version of foo.hpp
when they didn't before. Now you have all of the standard algorithms being included in your source file, and your using namespace std;
has pulled them all into the global namespace. std::fill()
now directly conflicts with your fill()
.
More insidious, you've gotten your code to compile by renaming your fill()
to xander333_fill()
, but something's not working right -- your report numbers are off. It turns out that your custom divides()
function, which does fixed precision math, is no longer being called because the templated function from (also newly included by foo.hpp
) makes for a better match because you're calling types did not exactly match the declared types.
Thread with relevant discussion is here:
http://www.cplusplus.com/forum/unices/27805/
There is no problem using using namespace std
in your source file when you make heavy use of the stl and know for sure that nothing will collide.
However, very often you don't need to use using namespace std
or not in the entire file:
Did you know you can:
void somefunction()
{
// Use it in a particular scope
using namespace std;
cout << "test" << endl;
}
a "good practice" that I am aware of is not to put using namespace
in include files, but be free to use it to your taste in your private .cpp files. I know people who like everything to be fully qualified, and some (like me) who assume that string
is an std::string
unless stated otherwise.
The reason for this is that if/when others use your include file (and this happens always), they are forced to accept your programming style.
Good luck!
链接地址: http://www.djcxy.com/p/29780.html下一篇: 是使用命名空间..就像坏?