1

Generally, the difference between ::any_name and any_name should be clear. If preceded by :: the name is always and only looked up in the global namespace.

I was wondering however whether there is an technical(*) difference, given a namespace that I already know to be toplevel (i.e. directly below the global namespace) and where I know that there is no second (nested) namespace (or any name) of the same name.

For example, is there any difference between using ::std::string vs. std::string?


(*) Readability, style and maintenance issues aside.

Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • Sure - if someone comes up with `namespace special { namespace std { } }` –  Aug 06 '14 at 20:26
  • 1
    @Dieter - If someone comes up with a nested namespace called `std` he should be shot. :-) (regardless of whether it's actually legal.) – Martin Ba Aug 06 '14 at 20:28
  • Try `std::vector<::std::string>` vs `std::vector` (C++03). – Johannes Schaub - litb Aug 06 '14 at 20:36
  • @JohannesSchaub-litb - sorry no `03` compiler around. Is there anything special about the snippet you commented? – Martin Ba Aug 06 '14 at 20:42
  • 2
    _@_MartinBa ' sorry no 03 compiler around.'_ What about setting the `-std=c++03` option? [See here please](http://coliru.stacked-crooked.com/a/3e6243755ea2844a), I think what @JohannesSchaub-litb wanted to point out is that older compilers might bail out for the `<::std::string>`, but this can be easily solved by putting an additional space `< ::std::string>` (same stupid thing, as with the closing `>>` angle brackets for nested templates). – πάντα ῥεῖ Aug 06 '14 at 21:08
  • @ πάντα - actually, it seems not the same as `>>` because `<:` seems to be interpreted as a digraph. But thanks a bunch for clearing that up. – Martin Ba Aug 06 '14 at 21:11
  • @MartinBa Could be right, but seems to be a problem of older standard implementations, [using `-std=c++11` it compiles fine](http://coliru.stacked-crooked.com/a/304909f76d1274ee). – πάντα ῥεῖ Aug 06 '14 at 21:13

1 Answers1

3

"For example, is there any difference between using ::std::string vs. std::string?"

Think about such weird situation

LegacyString.hpp

namespace Legacy {
    namespace std {
        class string {
            // ...
        };
    }
}

#include <string>
#include "LegacyString.hpp"

using namespace Legacy;

In this case it would certainly make a difference if you say ::std::string or just std::string in the following code.

And yes, as also stated in your other question's accepted answer, it's perfectly legal to name any construct, be it namespace, class, struct, typedef, etc. std.


As for your question edits:

"and where I know that there is no second (nested) namespace (or any name) of the same name."

If you know this is the case, the both forms are equivalent of course. As you mentioned the resolving of namespaces ends up at global level and will resolve it correctly without the prefixed ::.

IMHO usually we rely on, that at least no one is that stupid, to name any construct std actually. But to cite Einstein:

"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • It also applies to variables/functions in the global namespace (i.e. not in a `namespace { }` block) – Drew McGowen Aug 06 '14 at 20:33
  • I wasn't precise enough: "where I know that there is no second (nested) namespace (or any name) of the same name" – Martin Ba Aug 06 '14 at 20:33
  • @MartinBa Then it shouldn't make a difference of course, and that's what we usually rely on. Because of this I called my sample a ***weird situation***, and it's certainly not a good idea to name something `std` anywhere. – πάντα ῥεῖ Aug 06 '14 at 20:36