What does using ::fun mean when fun is a function?

Viewed 236
void fun(){
    std::cout << "hello" << std::endl;
}
namespace enc{
    using ::fun;
}

This code above gives no error. I want to understand what's going on here.

What does using ::fun; mean?

2 Answers

using a synonymous declaration introduces a name from one declarative region into another. In this case, the name fun is introduced into the namespace ::enc and it is synonymous with ::fun.

Breaking this down we have:

  • void fun() is a function that does not return a value (essentially nothing).

  • :: is a scope resolution operator. For example, cout and cin are defined in the std namespace. To qualify their names without using the using declaration we have to qualify them with std::cout and std::cin

To answer the main question, however:

  • using ::fun; is called a using declaration. A using declaration means that you can introduces namespace members into other namespaces, etc. This means that fun will now be visible as ::enc::fun;

For example:

void fun() {
    std::cout << "hello" << std::endl;
}
namespace enc {
    using ::fun;
}
void Reallyfun()
{
    enc::fun(); // Which calls ::fun
}
Related