g ++中的显式模板专门化导致麻烦 [英] Explicit template specialization in g++ causing troubles

查看:215
本文介绍了g ++中的显式模板专门化导致麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题从MSVC转换这段代码到g ++:

  #include< unordered_map> 
class A
{
template< class T> B类;

模板<>
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

  #include< unordered_map> 

class A
{
template< class T> B类;
模板<> class B< int>
std :: unordered_map< int,long,B< int>> m_Map;
};

模板<>
class A :: B< int>
{
std :: size_t operator()(int const& n)const {
}
};

int _tmain(int argc,_TCHAR * argv [])
{
A a;
return 0;
}

唉,现在VS给我一个错误



错误3错误C2079:'std :: _ Hash_oper1< true,_Hasher> :: _ Hashobj'使用未定义的类'A :: B& \\program files(x86)\microsoft visual studio 12.0 \vc\include\xhash

 错误2错误C2139:'A :: B< int>':不允许未定义的类作为参数编译器内部类型trait'__is_empty'c:\program files(x86)\microsoft visual studio 12.0 \vc\include\type_traits 

因此,无序映射definetely不想使用它所认为的未定义的类。即使我向前宣布它。
有没有人有一个线索是什么?谢谢。

解决方案

标准库容器不能用不完整类型声明为包含类型。这导致未定义的行为,没有诊断所需(意味着一些编译器可能看起来接受它,一些可能会拒绝它或在运行时表现奇怪)。



您必须使用来自不支持不完整类型的库(egboost)的哈希表,或重新设计代码。



一个简单的解决方法是使 B 不在 A 第一名


I've got the problem converting this piece of code to g++ from MSVC:

#include <unordered_map>
class A
{
    template <class T> class B;

    template<>
    class A::B<int>
    {

    };

    std::unordered_map<int, long, B<int>> m_Map;
};

Sure thing this is not standard c++, while VS still allows it GCC (g++) throws an error "Explicit specialization in non-namespace scope". Now I make it c++ compliant following the reference 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;
}

Alas, now VS gives me an error

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

So, unordered map definetely doesn't want to work with what it consideres an "undefined class". Even though I forward-declared it. Does anyone has a clue what is it all about? Thank you.

解决方案

Standard library containers can't be declared with incomplete types as the contained types. This causes undefined behaviour with no diagnostic required (meaning that some compilers may appear to accept it, and some may reject it or behave strangely at runtime).

You'll have to use a hash table from a different library (e.g.boost) that does support incomplete type, or redesign the code.

A simple fix would be to make class B not be declared within A in the first place

这篇关于g ++中的显式模板专门化导致麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆