A Workaround for Namespace-less Compilers

Since namespaces are relatively new in C++, some compilers do not support this feature yet. To workaround this lack of support, you can use struct holding static member functions:

 struct MY_PROJ{ // hide a function in a struct, rather than inject its name to the global namespace  static time_t Time() { return time(0); }}struct YOUR_PROJ {  static time_t Time() { return Date::time();} // different function with an identical name }

Of course, this workaround does not offer all of the benefits of real namespaces, such as argument dependent lookup, using declaration, and using directive. However, it can be used to avert name conflicts as in the example above.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: