我在将这段代码从 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.0\vc\include\xhash
and
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
因此,无序映射绝对不想使用它认为的“未定义类”。尽管我提前声明了它。
有谁知道这一切是怎么回事吗?谢谢。