Is there a possibility to have a using
directive whose scope is limited to a single class?
Note, that what I want to "use" is not contained in the parent of the current class.
For simplicity, assume the following exmple:
#include<vector>
class foo
{
using std::vector; //Error, a class-qualified name is required
}
Another interesting thing to know is, if the using
directives are included, if a header is included:
MyClassFoo.h:
#include<vector>
using std::vector; //OK
class foo
{
}
And in
NewHeader.h
#include "MyClassFoo.h"
...
how can I prevent "using std::vector
" to be visible here?
Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions, or to introduce enumerators into namespaces, block, and class scopes (since C++20).
A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A .
Use a using directive in an implementation file (i.e. *. cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace.
When you declare a program element such as a class, function, or variable, its name can only be "seen" and used in certain parts of your program. The context in which a name is visible is called its scope. For example, if you declare a variable x within a function, x is only visible within that function body.
Since you tagged c++11:
#include<vector>
class foo
{
template<typename T>
using vector = std::vector<T>;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With