Joseph
Joseph

Reputation: 1041

Is there any use in using "::member" to reference the global namespace?

In C++, members of a namespace myNamespace can be referenced as myNamespace::member.

However, there apparently exists a syntax similar to the one above for referencing the global namespace - simply ::member. Consider:

int foo() {
    return 4;
}

::foo(); // What is the difference between
foo();   // these two lines?

In short, is there a functional difference between using foo(); and ::foo(); in this scenario, or are they completely identical?

Upvotes: 0

Views: 57

Answers (1)

the4thamigo_uk
the4thamigo_uk

Reputation: 875

Example code :

#include <iostream>

void foo() {
  std::cout << 1;
}

namespace my_ns
{
  void foo() {
    std::cout << 2;
  }

  void goo1() {
    ::foo();
  }
  void goo2() {
    foo();
  }
}

int main(int c, char** args) {
  my_ns::goo1();
  my_ns::goo2();
  return 0;
}

Upvotes: 1

Related Questions