Okay, so if I use using namespace std;, does that mean the program will compile everything named under
namespace std? Many people recommend not to make the namespace std global, but some say it doesn't matter. But I usually do the second way, typing explicitly using::string, using::cout......until I read the newly released book from Andrei Alexandrescu
(highly repected person in the field), the book named C++ coding standard. (ISBN: 0-321-11358-6)
Here is a quote from the book:
But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way.
After reading this, I was thinking, am I doing the right way? So I thought I would ask people here how they are doing it.
// Implementation file
#include <iostream>
using namespace std;
int main()
{
// code
return 0;
}
or should I do this:
// Implemenation file
#include <iostream>
using::cout;
using::cin;
using::endl;
int main()
{
//code
return 0;
}
-
-
I usually do a "using namespace std" in source files (unless they are really small) and std::* in header files. It doesn't compile anything extra, just makes your life easier. The only way it can be bad is if you have custom classes with the same names as the STL classes, in which case you will have to fully use std::* to declare one.
-
I don't much care one way or the other in source files.
Never put any using declarations in header files though, since you force whomever will later use those headers to have their global namespace polluted. I'm working on an application whose source I've inherited, and there "using namespace std;" was put in the precompiled header, so either I'm stuck with it for any sourcefiles I add, or I have to change a few hundred existing source files.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.