"Using"仅在当前类上具有作用域的声明?

"Using" declaration with scope only on current class?

本文关键字:作用域 声明 Using      更新时间:2023-10-16

是否有可能有一个范围仅限于单个类的using指令?

请注意,我想"使用"的内容不包含在当前类的父类中。

为简单起见,假设以下示例如下:

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

另一个有趣的事情是,如果包含using指令,如果包含标头:

MyClassFoo.h:
#include<vector>
using std::vector; //OK
class foo
{
}

而在

NewHeader.h
#include "MyClassFoo.h"
...

如何防止"using std::vector"在这里可见?

既然你标记了 c++11:

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

至于你的第一个要求,你可以使用命名空间,以便using namespace的范围限制为单个类。

#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

对于第二种情况,请明智地利用#define#undef