Use a scope declaration for the current class only?

Is it possible to have a using directive, the scale of which is limited to one class?

Note that what I want to use is not contained in the parent of the current class.

For simplicity, suppose the following:

 #include<vector> class foo { using std::vector; //Error, a class-qualified name is required } 

Another interesting thing: if using directives are included, if the 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?

+6
source share
2 answers

Since you noted C ++ 11:

 #include<vector> class foo { template<typename T> using vector = std::vector<T>; }; 
+3
source

As for your first requirement, you can use a namespace so that the using namespace limited to one class.

 #include<vector> namespace FooClasses { using namespace std; //The scope of this statement will NOT go outside this namespace. class foo { vector<int> vecIntVector; }; }// namespace FooClasses 

In your second case, use #define and #undef wisely.

0
source

Source: https://habr.com/ru/post/979166/


All Articles