QMap
has both const and non-const find
methods:
iterator find(const Key & key)
const_iterator find(const Key & key) const
just like std::map
:
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
The QMap
iterators do differ in the constness of value()
:
T& QMap:: iterator::value() const;
const T& QMap::const_iterator::value() const;
So you shouldn't be able to manipulate the values through the returned iterator. But you assign the returned iterator to a ObjectMap::iterator
- I would expect an error at that point, but without a definition of ObjectMap
and its iterator, I'm unable to test that.
Certainly the following MCVE demonstrates a compile-time error:
#include <QMap>
int main()
{
const QMap<int,float> m{{1, 1.0}, {5, 5.0}};
auto it = m.find(1);
it.value() = 2.0;
// ^
// error: assignment of read-only location
// ‘it.QMap<Key, T>::const_iterator::value<int, float>()’
}
Assigning the iterator explicitly gives the other expected error:
#include <QMap>
int main()
{
const QMap<int,float> m{{1, 1.0}, {5, 5.0}};
QMap<int,float>::iterator it = m.find(1);
// ^
// error: conversion from ‘QMap<int, float>::const_iterator’
// to non-scalar type ‘QMap<int, float>::iterator’ requested
it.value() = 2.0;
}
(I compiled with g++ -std=c++11 -Wall -Wextra -fPIC $(pkg-config --cflags Qt5Core)
in both cases)
Edit
In the absence of a proper example in the question, I've tried to infer the missing declarations. But I still get the required error from gcc when I try to compile this:
#include <QMap>
#include <QUuid>
struct ObjectToTrack
{
};
struct ObjectProperties
{
ObjectProperties(const ObjectToTrack &obj)
: m_Obj(obj), m_Trackable(), m_Moved() {}
ObjectToTrack m_Obj;
bool m_Trackable;
bool m_Moved;
};
typedef QMap<QUuid, ObjectProperties> ObjectMap;
struct OpticalTrackingSystem
{
ObjectMap m_ObjectsToTrack;
void UpdateTrackability(const QUuid &uid) const;
};
void OpticalTrackingSystem::UpdateTrackability(const QUuid &uid) const
{
ObjectMap::iterator it = m_ObjectsToTrack.find(uid);
// ^
// error: conversion from ‘QMap<QUuid, ObjectProperties>::const_iterator’ to
// non-scalar type ‘QMap<QUuid, ObjectProperties>::iterator’ requested
it.value().m_Trackable = true;
it.value().m_Moved = false;
}
My conclusion is that there's nothing wrong with the code in the question. Either that compiler has a bug or there's important information missing from the question that's different to my reconstruction above.