I have seen both approaches in code. Could you explain what's the difference between the two? As I think it relates to the way namespace lookup is done by C++, could you provide some information on this as well, or perhaps a link to a good doc? Thanks.
-
5It's like the difference between `/usr/bin/rm` and `usr/bin/rm`; or between `Windows\calc.exe` and `C:\Windows\calc.exe`. – Kerrek SB Nov 27 '11 at 01:59
-
Is it? If I declare a namespace and use the identifier "std::cout" it will still work - however there is no "std" namespace nested in my own namespace. – Dan Nestor Nov 27 '11 at 02:05
-
It means global scope. See Mark's answer to this similar question: http://stackoverflow.com/questions/75213/scope-resolution-operator-without-a-scope – Dave M Nov 27 '11 at 02:09
-
1@DanNestor: Well, it's like that, plus the root directory is always in the search path, if you will :-) – Kerrek SB Nov 27 '11 at 02:27
2 Answers
Example:
#include <cstdio>
namespace x {
const int i = 1;
}
namespace y {
namespace x {
const int i = 2;
}
void func()
{
std::printf("x::i = %d\n", x::i);
std::printf("::x::i = %d\n", ::x::i);
}
}
int main()
{
y::func();
return 0;
}
Output:
x::i = 2 ::x::i = 1
Explanation:
When you refer to an identifier like
x::i
, the definition used is the "closest"x::i
. Inside::y::func
, the definition::y::x::i
is closer than the definition::x::i
. By contrast, there is no such function::y::std::printf
so::std::printf
is used instead.When you refer to an identifier like
::x::i
, there is no possible ambiguity: it looks for a top-level namespace namedx
, then finds ani
inside.
So using ::
at the beginning allows you to spell the full name of a global something. This also allows you to distinguish between local and global variables.
Example 2:
#include <cstdio>
const int x = 5;
int main()
{
const int x = 7;
std::printf("x = %d\n", x);
std::printf("::x = %d\n", ::x);
return 0;
}
Output:
x = 7 ::x = 5

- 205,541
- 37
- 345
- 415
-
Why does "std::printf" work then, inside namespace "y"? There is no namespace "std" nested inside namespace "y". Could you please explain this bit as well? – Dan Nestor Nov 27 '11 at 02:06
-
1@drandrestor: It's just like variable scoping - if the compiler doesn't find the name at the current level, it "pops" one level up and looks for it there. In this case the namespace `std` doesn't exists in `y`, so the compiler then checks the global namespace and find it there. – Ken Wayne VanderLinde Nov 27 '11 at 02:12
-
2@SethCarnegie: Ken Wayne VanderLinde seems to have mastered that particular art (see his gold badge). – Dietrich Epp Nov 27 '11 at 02:18
-
I accepted Ken's answer because he explained exactly that if a namespace is not found in the current scope, the parent scopes will then be searched. Many thanks to Dietrich as well for a very detailed answer. – Dan Nestor Nov 27 '11 at 02:42
It doesn't really matter, at least not most of the time.
In the format ::identifier1::identifier2
, the preceding colon says to look at global scope for identifier1
, and then look for identifier2
in that scope.
In the format identifier1::identifier2
, we instead look at the current scope for identifier1
. If we don't find it, then the parent scope is searched, and so on until we find it. We then search for identifier2
within whatever scope we just found.
In the case where you're already at global scope, it won't matter. But that story changes when you're working within a namespace or classes that has other namespaces or classes nested within it.

- 18,915
- 3
- 47
- 72