I think most C++ programmers here would agree that polluting the global namespace is a bad idea, but are there times when this rule can be ignored?
For example, I have a type that I need to use all over a particular application – should I define it thus:
mytypes.h typedef int MY_TYPE; foo.cpp MY_TYPE myType;
Or use a namespace:
mytypes.h namespace ns { typedef int MY_TYPE; } foo.cpp ns::MY_TYPE myType; ... using namespace ns; MY_TYPE myType;
Which do you prefer? Are there times when it is acceptable to use the first method?
I use namespaces for partitioning library code from application-specific code, and in a big project to partition the various modules that make up the project.
The global namespace is thus useful for application-specific types and functions that are used across multiple modules in the application.
So, if your
MY_TYPEis used throughout your application, put it in the global namespace, otherwise put it in a named namespace.