I'm writing a simply loop which iterates through the keys in a QMap as show below:
for (const auto &id: qAsConst(m_myMap.keys())) {
// Do something which does not affect m_myMap
}
Compiling the loop generates an error:
use of deleted function 'void qAsConst(const T&&)[with T=QList]'
Without the qAsConst I get the error:
Allocating an unneeded temporary container.
I thought the whole point of qAsConst was to avoid creating the temporary container. But I don' understand the message - why is it calling a deleted function? How do I fix this to get rid of the error / warning?
It means that you create a container with this statement:
m_myMap.keys()for no good reason.qAsConst()does not prevent the unneeded allocations.const T& QMap::const_iterator::operator*() constreturns a value, the range-loop with keys cannot be used.Use a usual loop like below
The first my answer is wrong.