:: without namespace

Hello,

recently I saw the following compiler error error: ‘::main’ must returnint.

That reminded my that long ago I wondered why sometimes you can find ::something. What does :: without a preceeding namespace mean?

I also used it a while ago, but I don't know why. And I don't do that anymore. (There is a post I made here where i write ::toupper, but I have no idea why I did it, and why I should do it :D)

Thanks in advance,
mathes
It explicitly refers to the global namespace; that is, identifiers that are not in any namespace. Otherwise, an identifier refers to the "most local" of the possibilities.

1
2
3
4
5
6
int i = 0;
int main() {
    int i = 1; // this i is said to "shadow" the outer i
    std::cout << "  i = " << i << "\n"; // 1
    std::cout << "::i = " << ::i << std::endl; // 0
}
Thank you very much!

Not only do I now understand, I also learned something new. Before I would have said "if you shadow the global i, you cannot access it anymore".

Regards,
mathes
Topic archived. No new replies allowed.