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++) 抛出错误 "Explicit specialization in non-namespace scope"。现在我按照参考 http://en.cppreference.com/w/cpp/language/template_specialization:
使它符合 c++
#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.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
因此,无序映射显然不想与它认为的 "undefined class" 一起工作。即使我转发声明它。
有谁知道这是怎么回事?谢谢。
标准库容器不能将不完整的类型声明为包含的类型。这会导致不需要诊断的未定义行为(意味着某些编译器可能会接受它,而某些编译器可能会拒绝它或在运行时表现异常)。
您必须使用来自支持不完整类型的不同库 (e.g.boost) 的散列 table,或者重新设计代码。
一个简单的解决方法是首先让 class B
不在 A
内声明
我在将这段代码从 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++) 抛出错误 "Explicit specialization in non-namespace scope"。现在我按照参考 http://en.cppreference.com/w/cpp/language/template_specialization:
使它符合 c++#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.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
因此,无序映射显然不想与它认为的 "undefined class" 一起工作。即使我转发声明它。 有谁知道这是怎么回事?谢谢。
标准库容器不能将不完整的类型声明为包含的类型。这会导致不需要诊断的未定义行为(意味着某些编译器可能会接受它,而某些编译器可能会拒绝它或在运行时表现异常)。
您必须使用来自支持不完整类型的不同库 (e.g.boost) 的散列 table,或者重新设计代码。
一个简单的解决方法是首先让 class B
不在 A
内声明