C++ – Do you prefer explicit namespaces or ‘using’ in C++


When using C++ namespaces, do you prefer to explicitly name them, like this:

std::cout << "Hello, world!\n";

Or do you prefer using namespace:

using namespace std;
cout << "Hello, world!\n";

And if if you prefer the latter, do you declare your usings at file or function scope?

Personally I prefer to explicitly name them – it's more typing but when using a mixture of namespaces (e.g. std and boost) I find it more readable.

Best Solution

I always use using namespace for std & boost. Everything else I tend to use an explicit namespace unless it is used so much that it would clutter up the code.

In headers, I never use using namespace to avoid polluting the global namespace of the #including source.