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!
You are trying to insert into an
unordered_mapthat you have declared to beconst, which is not allowed. Why is_cinsideLashared_ptr<const T>??? That effectively makes_mapon the object managed by the shared pointer a constant object an you will not be able to modify it.