Would you recommand prefixing global namespaces with ::
? (for instance ::std::cout
instead of std::cout
) Why? Is it faster to parse for the C++ compiler?
Thanks.
Only do this to disambiguate.
I have a piece of code where this is necessary since I’m in a namespace X
which has a function for a standard deviation – std
. Whenever I want to access the std
namespace, I need to use ::std
because otherwise the compiler will think that I am referring to said function.
Concrete example:
namespace X {
double std(::std::vector<double> const& values) { … }
void foo(::std::vector<double> const& values) {
::std::cout << std(values) << ::std::endl;
}
}
It has nothing to do with parsing speed. C++ uses Argument-dependent name lookup - Koenig Lookup and when you have to make sure that the compiler uses the symbol from the global root namespace you prefix it with ::
. If you don't the compiler might also use function definitions from other namespaces when it sees fit (depending on the lookup method).
So, it is better not to do it unless you have to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With