This is called using an ad. There are actually two ways to use the using keyword. There is a third special form of using declarations used within class definitions , but I will focus on the general declaration of use here. (see below).
They have two very different effects. Using an ad declares the name an alias for another ad or ad set (if you must name a set of overloaded functions). The name is declared in the current scope. That is, you can also use it inside blocks
int main() { using std::swap;
This is very useful if you use the name very often locally, and you do not want its prefix for all purposes, and is also useful when implementing swap using a context-specific search idium.
A using the directive , names the namespace and does not declare any names. Instead, he will change the name search to find names that are not actually pronounced where they think. To search for an unqualified name, he finds the names declared in the encompassing namespace, which includes both the usage directive and the target namespace. All names declared in the target namespaces will be found:
int cout; int main() { using namespace std;
Here cout will be considered declared twice in the global namespace and will cause ambiguity ( :: covers both main and std ). In a qualified namelookup, he will build a transitive closure of the namespace with all namespaces named using directives.
using namespace foo; int main() { ::c++; }
c not only viewed in the global namespace, but also in the foo namespace and in the namespaces in which foo uses directives for and so on. If, however, the global namespace contains a direct declaration (including a declaration of use), this declaration will hide declarations found indirectly using directives:
using namespace foo; int c; int main() { ::c++;
Use of declarations can appear in many places, including definitions within a class. Its meaning is similar to its value, elsewhere with an important limitation: it declares the name an alias for one or more declarations, but the declarations must be members of the base class. This is very useful for making names visible in a derived class that would otherwise be hidden with the same name as there.
struct base { void f(); }; struct derived : base { using base::f;
Now you can call df() . If there wasnβt an ad with an ad, then a search by name could find only one declaration f in derived and stop the search, and not enter the scope of the base class:
derived d; df(); // invalid without the using declaration df(0); // valid with or without the using declaration // explicitly starting lookup in base: valid with or without the using declaration d.base::f();
It also allows you to change the accessibility of base class members, although you should use it sparingly :)
In practice, I found it useful to re-view the virtual member function:
struct base { virtual void f(); virtual void f(int); }; struct derived : base {
Oops - now df(0); is invalid because a name search only finds the n parameter f ! A usage directive would solve it. Note that if you declare a function declaration that has the same parameter types and constant as the explicit declaration (for example, f() in this case), then the explicit declaration will still hide the one used in the usage declaration, - so both In this case, the functions f() will not conflict.
An alternative to this is to use a non-virtual idiom interface
struct base { void f() { do_f(); } void f(int) { do_f(0); } private: virtual void do_f(); virtual void do_f(int); }; struct derived : base { private: virtual void do_f() { ... } }; struct derived1 : derived { private: virtual void do_f(int) { ... } };
Now both df(0) and df() valid no matter what object you name it on.