qAsConst() deprecated
-
Seeing that qAsConst() is now deprecated, I have this question about preventing detachment.
Given
QList<Edge> edges;for (auto & edge : std::as_const(edges)) { doSomething(edge); } for (const auto & edge : edges) { doSomething(edge); }Are these two ways doing exactly the same thing, or does the second way detach?
-
Seeing that qAsConst() is now deprecated, I have this question about preventing detachment.
Given
QList<Edge> edges;for (auto & edge : std::as_const(edges)) { doSomething(edge); } for (const auto & edge : edges) { doSomething(edge); }Are these two ways doing exactly the same thing, or does the second way detach?
@ChortleMortal Second one detaches. It's equivalent to
for (auto it = edges.begin(); it != edges.end(); ++it) { const auto& edge = *it; doSomething(edge); }It doesn't matter if
edgeis const or not. Detach happens on the call tobegin()on non-const container, so before the assignment to the variable even takes place. -
@ChortleMortal Second one detaches. It's equivalent to
for (auto it = edges.begin(); it != edges.end(); ++it) { const auto& edge = *it; doSomething(edge); }It doesn't matter if
edgeis const or not. Detach happens on the call tobegin()on non-const container, so before the assignment to the variable even takes place.@Chris-Kawa
Thanks so much Chris. You have saved me from making a systematic errror. Much appreciated. -
C ChortleMortal has marked this topic as solved on