scope resolution operator without a scope

C++

C++ Problem Overview


In C++, what is the purpose of the scope resolution operator when used without a scope? For instance:

::foo();

C++ Solutions


Solution 1 - C++

It means global scope. You might need to use this operator when you have conflicting functions or variables in the same scope and you need to use a global one. You might have something like:

void bar();    // this is a global function

class foo {
    void some_func() { ::bar(); }    // this function is calling the global bar() and not the class version
    void bar();                      // this is a class member
};

If you need to call the global bar() function from within a class member function, you should use ::bar() to get to the global version of the function.

Solution 2 - C++

A name that begins with the scope resolution operator (::) is looked up in the global namespace. We can see this by looking at the draft C++ standard section 3.4.3 Qualified name lookup paragraph 4 which says (emphasis mine):

>A name prefixed by the unary scope operator :: (5.1) is looked up in global scope, in the translation unit where it is used. The name shall be declared in global namespace scope or shall be a name whose declaration is visible in global scope because of a using-directive (3.4.3.2). The use of :: allows a global name to be referred to even if its identifier has been hidden (3.3.10).

As the standard states this allows us to use names from the global namespace that would otherwise be hidden, the example from the linked document is as follows:

int count = 0;

int main(void) {
  int count = 0;
  ::count = 1;  // set global count to 1
  count = 2;    // set local count to 2
  return 0;
}

The wording is very similar going back to N1804 which is the earliest draft standard available.

Solution 3 - C++

Also you should note, that name resolution happens before overload resolution. So if there is something with the same name in your current scope then it will stop looking for other names and try to use them.

void bar() {};
class foo {
    void bar(int) {};
    void foobar() { bar(); } // won't compile needs ::bar()
    void foobar(int i) { bar(i); } // ok
}

Solution 4 - C++

When you already have a function named foo() in your local scope but you need to access the one in the global scope.

Solution 5 - C++

My c++ is rusty but I believe if you have a function declared in the local scope, such as foo() and one at global scope, foo() refers to the local one. ::foo() will refer to the global one.

Solution 6 - C++

referring to the global scope

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLandon KuhnView Question on Stackoverflow
Solution 1 - C++MarkView Answer on Stackoverflow
Solution 2 - C++Shafik YaghmourView Answer on Stackoverflow
Solution 3 - C++Matt PriceView Answer on Stackoverflow
Solution 4 - C++DrealmerView Answer on Stackoverflow
Solution 5 - C++itsmattView Answer on Stackoverflow
Solution 6 - C++shooshView Answer on Stackoverflow