Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Using" declaration with scope only on current class?

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?

like image 942
S.H Avatar asked Dec 05 '14 09:12

S.H


People also ask

What is the scope of using declaration?

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).

What is the use of using declaration in C ++?

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 .

What are is difference between using namespace directive and using the using declaration for accessing namespace members?

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.

What is scope of a class?

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.


1 Answers

Since you tagged c++11:

#include<vector>

class foo
{
  template<typename T>
  using vector = std::vector<T>;
};
like image 133
Drax Avatar answered Sep 27 '22 23:09

Drax