Like :
using ::size_t; using ::fpos_t; using ::FILE;
In fact it’s a question inspired by the comment under this question:
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is called using declaration. There are actually two ways you can use the
usingkeyword. There is a third special form of using declarations used inside class definitions,but i’ll focus on the general using declaration here.(see below).These have two very different effects. A using declaration declares a name to be an alias to another declaration or a set of declarations (if you were to name a set of overloaded functions). The name is declared in the current scope. That is, you can use it inside blocks too
This is quite useful if you use a name very often locally and you don’t want to prefix it in all uses, and it’s also useful in implementing the swap using argment dependent lookup idiom.
A using directive names a namespace and does not declare any names. Instead it will modify name lookup to find names that aren’t really declared where it thinks they are. For unqualified name lookup, it find names declared in the enclosing namespace that encloses both the using directive and the target namespace. All names that are declared in the target namespaces will be found:
Here,
coutwill be thought as being declared twice in the global namespace, and causes an ambiguity (::encloses bothmainandstd). In qualified namelookup, it will build the transitive closure of a namespace with all the namespaces named in using directives.cis not only looked up in the global namespace, but also in the namespacefooand in the namespaces thatfoohas using directives for and so on. If however the global namespace would contain a direct declaration (including a using declaration), that declaration will hide the declarations found indirectly by using directives:Using declarations can appear in many places, including inside class definitions. Its meaning is similar to its meaning otherwhere with an important restriction: It declares a name to be an alias to one or more declarations, but the declarations must be members of a base class. This is very useful for making names visible in a derived class that would otherwise be hidden by the same name declared there
Now you can call
d.f(). If there were no using declaration, then name lookup would only find one declaration offinderivedand stop lookup, not delving into the base class scope:It also allows to change the accessibility of base-class members, although you should use that sparingly 🙂
In practice, i found it useful for making virtual member function re-visible:
Oops – now
d.f(0);is invalid because name lookup only finds the zero parameterf! The using directive would solve it. Notice that if you alias a function declaration that has the same parameter types and constness as an explicit declaration (likef()in this case), then the explicit declaration will still hide the one that the using declaration is an alias for – so bothf()functions won’t conflict in this case.An alternative to solve this is using the non-virtual interface idiom
Now, both
d.f(0)andd.f()are valid no matter on what object you call it.