"Don't call QList::front() on a temporary" clazy warning, what is it about?
-
The following code seems perfectly valid to me, but produces a clazy warning:
openLogFile(mimeData->urls().front().toLocalFile());
The warning is "don't call QList::front() on a temporary". I don't understand it, what's the problem? Is there actually a problem?
I'm pretty sure that all the temporaries here have the correct life span for this to work properly, and eventually the call chain results in a
QString
that's passed to the function as argument.P. S. The
QList::front()
method is not even ref-qualified. If it was invalid for temporaries they could have&
-qualified it, or provided a&&
overload that is valid.
P. P. S. Thefront()
method returns a non-const reference (as it should), which would be an error if I tried to modify it, but if that's what it is about then the wording of the warning is confusing. -
@Christian-Ehrlicher A deep copy of the whole container? Or just the single front element?
@Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:
A deep copy of the whole container?
... the container needs to be detached and a deep copy might be generated.
-
The following code seems perfectly valid to me, but produces a clazy warning:
openLogFile(mimeData->urls().front().toLocalFile());
The warning is "don't call QList::front() on a temporary". I don't understand it, what's the problem? Is there actually a problem?
I'm pretty sure that all the temporaries here have the correct life span for this to work properly, and eventually the call chain results in a
QString
that's passed to the function as argument.P. S. The
QList::front()
method is not even ref-qualified. If it was invalid for temporaries they could have&
-qualified it, or provided a&&
overload that is valid.
P. P. S. Thefront()
method returns a non-const reference (as it should), which would be an error if I tried to modify it, but if that's what it is about then the wording of the warning is confusing.mimeData->urls()
returns an object so it's a temporary. A temporary is by default non-const so since you're calling (the non-const) front() on it, the container needs to be detached and a deep copy might be generated. And that's what the warning is about.
Use e.g. constFirst() or store the return ofmimeData->urls()
in a local variable first. -
mimeData->urls()
returns an object so it's a temporary. A temporary is by default non-const so since you're calling (the non-const) front() on it, the container needs to be detached and a deep copy might be generated. And that's what the warning is about.
Use e.g. constFirst() or store the return ofmimeData->urls()
in a local variable first.@Christian-Ehrlicher A deep copy of the whole container? Or just the single front element?
-
@Christian-Ehrlicher A deep copy of the whole container? Or just the single front element?
@Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:
A deep copy of the whole container?
... the container needs to be detached and a deep copy might be generated.
-
Thanks for the explanation. That's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer
std
facilities over Qt and do not use Qt classes in performance-critical code. -
-
Thanks for the explanation. That's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer
std
facilities over Qt and do not use Qt classes in performance-critical code.@Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:
hat's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer std facilities over Qt and do not use Qt classes in performance-critical code.
I don't understand what you're trying to tell us here.
What's odd? CopyOnWrite is a design decission by Qt and you have to take care for it.
With plain std you have the problem that the whole container would be copied anyway (forgetting a possible rvo).
-
@Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:
hat's a very odd design and I don't understand why they would implement it this way. Yet another lesson to always prefer std facilities over Qt and do not use Qt classes in performance-critical code.
I don't understand what you're trying to tell us here.
What's odd? CopyOnWrite is a design decission by Qt and you have to take care for it.
With plain std you have the problem that the whole container would be copied anyway (forgetting a possible rvo).
@Christian-Ehrlicher, calling
front()
can never create a copy of anstd
container. But I finally understood just now why it does in Qt. -
@Christian-Ehrlicher, calling
front()
can never create a copy of anstd
container. But I finally understood just now why it does in Qt.@Violet-Giraffe said in "Don't call QList::front() on a temporary" clazy warning, what is it about?:
calling front() can never create a copy of an std container.
Yes because the copy occoured already before.