S.H
S.H

Reputation: 945

"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?

Upvotes: 8

Views: 3186

Answers (2)

Drax
Drax

Reputation: 13288

Since you tagged c++11:

#include<vector>

class foo
{
  template<typename T>
  using vector = std::vector<T>;
};

Upvotes: 4

MNS
MNS

Reputation: 1394

As for your first requirement, you can use a namespace so that the scope of using namespace is limited to a single 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

For your second case, do make use of #define and #undef wisely.

Upvotes: 0

Related Questions