If name in C++ is not fully qualified, e.g. std::cout, it can lead to an unintentional error, such as mentioned at https://en.cppreference.com/w/cpp/language/qualified_lookup. But using a fully qualified name for ::std namespace, e.q. ::std::cout, is very rare, as I have noticed.
Is there any reason why a fully qualified name for ::std namespace is not used?
And what about using fully qualified name for own created namespaces? Is it good idea?
You are completely right, in the sense that
yyyy::xxxcan be ambiguous if there is a namespaceyyyyand also a classyyyywhich are both visible in the same scope. In this case only the full qualification::yyyy::xxxcan solve the ambiguity. The example of your link makes it very clear:But in practice, it's difficult to create a conflicting
stdat top level, since most of the includes from the standard library will make it fail:This means that to create a conflict, you'd need to define local classes or introduce a
usingclause in another namespace. In addition, nobody will (dare to) call a classstd.Finally, in practice,
::yyyy::xxxis less convenient to read. All this explains why you won't find it very often.Additional remark
The problem is not so much for
stdwhich is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to:::yyyyto disambiguate:Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace
::foowould be used.