There seem to be different views on using \'using\' with respect to the std namespace.
Some say use \' using namespace std\', other say don\'t but rathe
Most C++ users are quite happy reading std::string, std::vector, etc. In fact, seeing a raw vector makes me wonder if this is the std::vector or a different user-defined vector.
I am always against using using namespace std;. It imports all sorts of names into the global namespace and can cause all sorts of non-obvious ambiguities.
Here are some common identifiers that are in the std namespace: count, sort, find, equal, reverse. Having a local variable called count means that using namespace std won't enable you to use count instead of std::count.
The classic example of an unwanted name conflict is something like the following. Imagine that you are a beginner and don't know about std::count. Imagine that you are either using something else in or it's been pulled in by a seemingly unrelated header.
#include
using namespace std;
int count = 0;
int increment()
{
return ++count; // error, identifier count is ambiguous
}
The error is typically long and unfriendly because std::count is a template with some long nested types.
This is OK though, because std::count goes into the global namespace and the function count hides it.
#include
using namespace std;
int increment()
{
static int count = 0;
return ++count;
}
Perhaps slightly surprisingly, this is OK. Identifiers imported into a declarative scope appear in the common namespace that encloses both where they are defined and where they are imported into. In other words, std::count is visible as count in the global namespace, but only inside increment.
#include
int increment()
{
using namespace std;
static int count = 0;
return ++count;
}
And for similar reasons, count is ambiguous here. using namespace std doesn't cause std::count, hide the outer count as it might be expected. The using namespace rule means that std::count looks (in the increment function) as though it was declared at the global scope, i.e. at the same scope as int count = 0; and hence causing the ambiguity.
#include
int count = 0;
int increment()
{
using namespace std;
return ++count; // error ambiguous
}