Reputation: 1790
Exact Duplicate: Do you prefer explicit namespaces or ‘using’ in C++?
Which of these is a preferred convention for using any namespace?
using namespace std;
or
using std::cin;
using std::cout;
or
calling the function as and when required in the code?
std::cout<<"Hello World!"<<std::endl;
Upvotes: 11
Views: 1615
Reputation: 1223
I always list out the full namespace. It improves readability and lets other people know where your functions and data are coming from. using
is very annoying when reading other peoples' code, especially when I'm trying to learn something, because I can't tell if it's part of that namespace or the other. And like these other sensible people are saying, it does defeat the whole point of namespacing, doesn't it? The point is to keep everything separate and give meaning to the data and logic.
A good order to remember: the most important person is the client who uses the program; second most important is other coders who are either maintaining or trying to learn from your code; least important is you. :-)
Upvotes: 1
Reputation:
This has been asked many times before, but my SO search fu seems to have deserted me for the moment. Basically:
Never put a using directive of any sort in a header file – doing so will contaminate your code and introduce all sorts of hard to track down bugs.
Prefer a using declaration, like using std::string
, in an implementation (.cpp) file that makes heavy use of such a type.
As a last resort, use using namespace std
, but only in implementation files – I use it in posts of compilable code on SO, for convenience.
Upvotes: 12
Reputation: 1432
It's one thing to recommend writing out the fully qualified name when the namespace is std, where std:: only adds 5 extra characters. It's a whole 'nother issue when namespaces stack, and you are faced with writing something like:
if (NumberOfInstances > 0 && (code_type == MyNamespace::MyLongButMeaningfulClassName::EnumDefinedWithinClass::CODE_GREEN ||
code_type == MyNamespace::MyLongButMeaningfulClassName::EnumDefinedWithinClass::CODE_YELLOW)) {
especially if you have a company style guidline limiting lines to 80 characters and you add a few more indents. Something like that obscures the logic of the code behind all the verbiage. At that point, you start to appreciate using and/or local namespace aliases, in the interest of readability.
Upvotes: 0
Reputation: 44444
I personally prefer the third option. Just have a look at this:
namespace A { int a=0; }
namespace B { int a=0; }
and you use it as:
using namespace A;
using namespace B;
using namespace std;
cout<<a<<endl; //error here!
Instead if you could just say,
std::cout<<B::a<<std::endl; //No problem, more readable
Although it might take a bit more time to type the code out, the second and the third options are more (kind of) preferred.
Upvotes: 3
Reputation: 9508
I'd just like to point out that using namespace foo
is scoped. Example:
#include <iostream>
#include <vector>
//...
for (int i=0; i<10; ++i)
{
using namespace std;
cout << i << endl;
}
vector v; // won't compile
When used with care, using namespace std
can be useful and harmless at the same time.
Upvotes: 1
Reputation: 55445
This is a modified version of another answer I wrote on the same subject. Enough of these questions and maybe I will end up with a definitive post ;)
The major issue is name conflicts, in that if you have a variable called cout in your code and you are using namespace std;
it will be ambiguous as to what you mean. It isn't just cout
. count
, reverse
and equal
will also be included, which are all common identifiers.
Disregarding all problems to the compiler, it is also a problem for anyone coming to read your code. Those extra 5 characters ensure that the next person maintaining your code knowns exactly what you mean.
It is also worth noting that you should never put
using namespace std;
In a header file, as it can propagate to all files that include that header file, even if they don't want to use that namespace. Another problem here is that it is also not clear that the std namespace has been imported, so the maintainer (or you in 3 months time) adds a variable with the same name as some obscure std function that was included into the same compilation unit and then spends an hour trying to find the cause of the compilation error.
In the majority of cases it is very beneficial to use things like
using std::swap
As if there is a specialized version of swap, the compiler will use that, otherwise it will fall back on std::swap
. If you call std::swap
, you always use the basic version, which will not call the specialized version (even if it exists).
Take for example code using the pimpl idiom. Where as the default copy may copy all the data in the actual implementation, where as all that needs doing is swapping the pointers. Using a specialized swap could save huge amounts of execution time, and well designed libraries should specialize it.
In summary,
Always prefer using std::swap
over std::swap()
Avoid using namespace std
in a header at all costs due to propagation, try to avoid using it in implementation files.
Having thousands of using std::foo
at the top of every file is not the way to go. At most use it for commonly use classes.
Everything else is opinion.
Upvotes: 8
Reputation: 92056
A very good explanation is given here.
The first style i.e. using namespace whatever defeats the whole purpose of namespacing. You should never be using it except in small code snippets. (I don't use it there either! :D )
Second one is way too verbose. Not practical.
I personally like the third style i.e. typing out the fully qualified name (e.g. std::cout).
Remember, the code is read much more times than it is written & using fully qualified names IMO makes your code more readable.
Upvotes: 14
Reputation: 96879
It is a matter of style, but for one thing: You should never import a namespace into the global scope. e.g.:
#include <iostream>
using namespace std; // Pollution!
int main()
{
....
}
If you want to import the namespace just import it to the scope where you are working:
#include <iostream>
int main()
{
using namespace std; // Good!
....
}
Upvotes: 1
Reputation: 67
Whatever you prefer. Really, it doesn't matter. However, most code-snippets use
using namespace std;
Upvotes: -4