In C++, should we be prepending stuff in the global namespace with ::
?
For example, when using WinAPI, which is in C, should I do ::HANDLE
instead of HANDLE
, and ::LoadLibrary
instead of LoadLibrary
? What does C++ say about this? Is it generally a good idea, factoring in issues like readability and maintainability?
Names in C++ can be qualified and unqualified. There are different rules for qualified and unqualified name lookup. ::HANDLE
is a qualified name, whereas HANDLE
is an unqualified name. Consider the following example:
#include <windows.h>
int main()
{
int HANDLE;
HANDLE x; //ERROR HANDLE IS NOT A TYPE
::HANDLE y; //OK, qualified name lookup finds the global HANDLE
}
I think that the desicion of choosing HANDLE
vs. ::HANDLE
is a matter of coding style. Of course, as my example suggests, there might be situations where qualifying is mandatory. So, you might as well use ::
just in case, unless the syntax is somewhat disgusting for you.