【发布时间】:2015-12-06 20:39:30
【问题描述】:
我在将这段代码从 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++) 抛出错误“非命名空间范围内的显式特化”。现在我按照参考 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
因此,无序映射绝对不想使用它认为是“未定义类”的东西。即使我提前声明了它。 有谁知道这一切是怎么回事?谢谢。
【问题讨论】:
-
一个简单的解决方法是首先让
class B不在A中声明 -
谢谢你,这个作品,你可以发布作为答案。