Unsolved Clazy complaining about detaching on temporary
-
Hi,
In the following code:
I have the following method:QList<Element> getElems();
At some point, I want to iterate over the return value from the method above. Something like:
for (const auto& elem : getElems()) { //<--- clazy warns in this line about possible detaching. doSomething(elem); }
The violation seems to be valid. It's indeed possible here to detach if my understanding is correct. . For regular l-value fix would be to add qAsConst(elems) and loop over the result of this. but
qAsConst(T&&) = delete;
I can't use on a return value from a method getElems().
The only solution I see here is const auto elems = getElems() in the line before looping. Are there other ways to avoid detaching here? What is the suggested way of dealing with this situation?
Thank you
-
Hi,
You should provide more code so that people can understand better what this is all about.
-
Hi @SGaist .
I've added more information to the original question. Hope that makes it clearer. Thank you -
Ok, so it's a free function ?
Out of curiosity, what is the issue with using a variable ?
-
@SGaist Thanks for your reply
The problem with using a variable is polluting code with variables and it's more natural to call a method directly in a loop so many developers are doing this without thinking too much. It's fine for not Qt containers.
Making signatureconst QList<Element> getElems()
seems to solve the issue with clazy at least. Is this a good solution?
The general goal is to use as many structures in a consistent way as possible. It makes the code clearer and easier to read and write.