Friday 24 March 2017

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.


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...