I want to know if this code is safe and doesnt have any undefined behavior.
QueueMap::const_iterator it = m_3playersQueue.find(s->m_GameCode);
if(it == m_3playersQueue.end())
{
std::deque<Session*> stack;
stack.push_back(s);
m_3playersQueue[s->m_GameCode] = stack;
return;
}
const_cast<std::deque<Session*>&>(it->second).push_back(s);
QueueMap is of type std::tr1::unordered_map< uint32, std::deque<Session*> >
Yes, it is safe. Even if a
const_iteratoris different from aniterator, the objects in the map are the same. As long as all the map contents were created as mutablestd::deque<Session*>objects, it’s OK to cast the constness away.Of course, it’s possible you’re violating some invariant the constness was meant to convey, but that’s possible with any
const_casttaken in isolation.