I would like to return a const reference to a QMap value. From what I can understand, QMap is special in that if you try to access a key that does not exist, it will create a value with the default constructor and return it. But if I still understand correctly, the reference will be temporary and therefore I get the warning.
I have this piece of code:
const T &getSelectionDesc(QListWidgetItem *item)
{
if (!indexes.contains(item))
indexes.insert(item, T(item->text()));
return indexes.value(item);
}
As you can see, I've already made sure that the key returns something, I create the object the first time it is required and then save it in the QMap for ulterior uses.
Despite that, I still get the warning, what should I change here to correct the behavior?
Edit:
Here is how I've defined indexes
:
QMap<QListWidgetItem *, T> indexes;
This is the warning I'm getting:
In instantiation of 'const T& SelectListDialog::getSelectionDesc(QListWidgetItem*) [with T = BackgroundDesc]':
warning: returning reference to temporary [-Wreturn-local-addr]
return indexes.value(item);