dimanche 6 décembre 2015

Explicit template specialization in g++ causing troubles

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

#include <unordered_map>
class A
{
    struct X {

    };

    template <class T> class B;

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

    };

    std::unordered_map<X, long, B<X>> 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://ift.tt/1IQWg8G:

#include <unordered_map>
class A
{
    struct X {

    };

    template <class T> class B;
    template <> class B<X>;
    std::unordered_map<X, long, B<X>> m_Map;
};

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

};

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<A::X>'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\xhash

and

Error   2   error C2139: 'A::B<A::X>' : 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.

Aucun commentaire:

Enregistrer un commentaire