A Workaround for Namespace-less Compilers

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.

See also  5 Tips for Working With an Onsite Interpreter

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist