C++ &引用;使用;仅在当前类上具有作用域的声明?
是否有可能使用范围仅限于单个类的指令执行C++ &引用;使用;仅在当前类上具有作用域的声明?,c++,c++11,using,using-directives,C++,C++11,Using,Using Directives,是否有可能使用范围仅限于单个类的指令执行 请注意,我想要“使用”的内容不包含在当前类的父类中 为简单起见,假设以下示例: #include<vector> class foo { using std::vector; //Error, a class-qualified name is required } 而且 NewHeader.h #include "MyClassFoo.h" ... 如何防止“使用std::vector”在此处可见?对于您的第一个要求,您可以使用命名空
请注意,我想要“使用”的内容不包含在当前类的父类中
为简单起见,假设以下示例:
#include<vector>
class foo
{
using std::vector; //Error, a class-qualified name is required
}
而且
NewHeader.h
#include "MyClassFoo.h"
...
如何防止“使用std::vector
”在此处可见?对于您的第一个要求,您可以使用命名空间,以便使用命名空间的范围仅限于单个类
#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
#包括
命名空间类
{
使用命名空间std;//此语句的范围不会超出此命名空间。
福班
{
向量向量;
};
}//命名空间类
对于第二种情况,请明智地使用#define
和#undef
。因为您标记了c++11:
#include<vector>
class foo
{
template<typename T>
using vector = std::vector<T>;
};
#包括
福班
{
模板
使用vector=std::vector;
};
是否有可能为“std::vector”创建模板化类型别名,然后称为“vector”?这样做与我希望using std::vector在类中所做的一样是的,using
声明可以在创建类型别名时模板化,可以调用该类型别名vector
,因为它位于std::vector
的另一个作用域中。我认为值得指出的是,这不是using指令,这是一个使用声明。使用指令引入名称空间。谢谢,我将“指令”改为“声明”,接近使用向量所需的内容。另外,template Using vector=std::vector代码>如果您有任何使用可选分配器的倾向。这看起来不错,但不幸的是它给了我“无法识别的模板声明/定义”@S.H您需要使用-std=c++11和足够新的编译器来编译。我使用c++11编译,VS2012->和支持的VS c++11是一个问题features@S.H是的,它似乎是在VS2013中引入的:,悲哀=/
#include<vector>
class foo
{
template<typename T>
using vector = std::vector<T>;
};