Monday 21 October 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...