Please watch this:
template<typename TK, typename TV>
class MetaAssociator
{
public:
void Set(TK key, TV const & value)
{
boost::lock_guard<boost::mutex> lock(m_Mutex);
m_Map[key] = value;
}
TV Get(TK key) const
{
boost::lock_guard<boost::mutex> lock(m_Mutex);
std::map<TK,TV>::const_iterator iter = m_Map.find(key);
return iter == m_Map.end() ? TV() : iter->second;
}
private:
mutable boost::mutex m_Mutex;
std::map<TK,TV> m_Map;
};
When I change the std::map<TK,TV>::const_iterator iter to std::map<TK,TV>::iterator it is causing the following compile error:
error C2440: 'initializing' : cannot convert from stlpd_std::priv::_DBG_iter<_Container,_Traits> to stlpd_std::priv::_DBG_iter<_Container,_Traits>
Can anyone exmplain why? I am not modifying the m_Map. Why the compiler is complaining??
findgives you back aconst_iterator. If it worked as you wish, then you would be able to change it. The object is kept safe against changes via type of the variable, not tracking the actual usage.