g++中的显式模板专用化导致问题
Explicit template specialization in g++ causing troubles
我在将这段代码从MSVC:转换为g++时遇到了问题
#include <unordered_map>
class A
{
template <class T> class B;
template<>
class A::B<int>
{
};
std::unordered_map<int, long, B<int>> m_Map;
};
当然,这不是标准的c++,而VS仍然允许它GCC(g++)抛出一个错误"非命名空间范围中的显式专门化"。现在,我根据引用使其符合c++http://en.cppreference.com/w/cpp/language/template_specialization:
#include <unordered_map>
class A
{
template <class T> class B;
template <> class B<int>;
std::unordered_map<int, long, B<int>> m_Map;
};
template<>
class A::B<int>
{
std::size_t operator()(int const& n) const {
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
return 0;
}
唉,现在VS给了我一个错误
Error 3 error C2079: 'std::_Hash_oper1<true,_Hasher>::_Hashobj' uses undefined class 'A::B<int>' c:program files (x86)microsoft visual studio 12.0vcincludexhash
和
Error 2 error C2139: 'A::B<int>' : an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_empty' c:program files (x86)microsoft visual studio 12.0vcincludetype_traits
因此,无序映射定义不希望使用它认为的"未定义类"。尽管我提前宣布了。有人知道这是怎么回事吗?非常感谢。
标准库容器不能用不完整的类型声明为包含的类型。这会导致不需要诊断的未定义行为(这意味着一些编译器可能会接受它,而有些编译器可能会拒绝它或在运行时表现异常)。
您必须使用来自不同库(例如boost)的支持不完整类型的哈希表,或者重新设计代码。
一个简单的修复方法是首先使类B
不在A
内声明
相关文章:
- 警告处理为错误这里有什么问题
- .cpp和.h文件中的模板专用化声明
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- C++部分模板专用化问题
- C++隐式/显式模板方法专用化问题
- C++ GCC 的部分模板专用化问题
- 专用纯虚拟模板函数(未定义引用)的另一个问题
- 嵌套类模板专用化问题
- 显式模板专用化和依赖名称查找问题
- 模板专用化和enable_if问题
- 别名模板专用化问题
- 模板专用化问题
- 部分模板专用化有什么问题
- g++中的显式模板专用化导致问题
- C++结构成员模板函数的显式专用化 - 这是一个Visual Studio问题吗?
- 解决函数模板部分专用化问题