Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ g+中的显式模板专门化+;闹事_C++_C++11_Gcc_G++ - Fatal编程技术网

C++ g+中的显式模板专门化+;闹事

C++ g+中的显式模板专门化+;闹事,c++,c++11,gcc,g++,C++,C++11,Gcc,G++,我在将这段代码从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; }; #包括 甲级 { 模板B类; 模板

我在将这段代码从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;
};
#包括
甲级
{
模板B类;
模板
A类::B类
{
};
std::无序图m\u图;
};
<>当然,这不是标准C++,而VS仍然允许GCC(g++)在“非命名空间范围”中抛出一个“显式专用化”错误。现在我让C++遵循以下的引用:

#包括
甲级
{
模板B类;
模板B类;
std::无序图m\u图;
};
模板
A类::B类
{
std::size\u t运算符()(int const&n)const{
}
};
int _tmain(int argc,_TCHAR*argv[]
{
A A;
返回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.0\vc\include\xhash
Error 3 Error C2079:'std::_Hash_oper1::_Hashobj'使用未定义的类'A::B'c:\program files(x86)\microsoft visual studio 12.0\vc\include\xhash

Error 2 Error C2139:'A::B':不允许未定义的类作为编译器内部类型trait'\uu为空'c:\program files(x86)\microsoft visual studio 12.0\vc\include\type\u traits的参数
所以,无序映射显然不想使用它所认为的“未定义类”。尽管我已经宣布了。
有人知道这是怎么回事吗?谢谢。

标准库容器不能声明为包含不完整类型。这会导致不需要诊断的未定义行为(意味着某些编译器可能会接受它,而某些编译器可能会拒绝它或在运行时表现异常)

您必须使用另一个库(例如boost)中的哈希表来支持不完整类型,或者重新设计代码


一个简单的解决办法是首先使
B类
不在
A
内声明

一个简单的解决办法是首先使
B类
不在
A
内声明谢谢,这很有效,您可以作为答案发布。我认为不太可能有支持不完整哈希器的实现。他们使用不完整类型
B
作为第三个模板参数-哈希器。这是可以理解的,因为实现几乎肯定会直接存储哈希器(可能包装在一些支持EBO的机器中)。
Error   3   error C2079: 'std::_Hash_oper1<true,_Hasher>::_Hashobj' uses undefined class 'A::B<int>'    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xhash
 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.0\vc\include\type_traits