Search code examples
c++insertqualifiers

error C2663: 'std::_Hash<_Traits>::insert' : 3 overloads have no legal conversion for 'this' pointer


Consider the following code:

#include <unordered_map>

struct A {};

struct T
{
    std::unordered_map<std::string, A> _map;
};

struct L
{
    std::shared_ptr<const T> _c;
};


class f {
    void oid (std::shared_ptr<L> l, std::string st, A a) {
        l->_c->_map.insert(std::make_pair(st,a));
    }
};

During compilation, it throws the following error:

error C2663: 'std::_Hash<_Traits>::insert' : 3 overloads have no legal conversion for 'this' pointer with [ _Traits=std::tr1::_Umap_traits,std::equal_to>,std::allocator>,false> ]

I tryed to remove the const from std::shared_ptr<const T> _c; (not that I think it matters), but it constructs some other errors...

Thanks for your help!


Solution

  • You are trying to insert into an unordered_map that you have declared to be const, which is not allowed. Why is _c inside L a shared_ptr<const T>??? That effectively makes _map on the object managed by the shared pointer a constant object an you will not be able to modify it.