D25039: Fix Clazy performance issues, const &

Friedrich W. H. Kossebau noreply at phabricator.kde.org
Thu Oct 31 16:35:39 GMT 2019


kossebau added inline comments.

INLINE COMMENTS

> ahmadsamir wrote in ftp.cpp:1376
> IIUC, the compiler will use a temporary object to hold the return of tempurl.fileName().
> 
> The temporary is used to initialize filename, and then it's gone:
> const QString filename = tempurl.filename();
> 
> filename here is a reference-to-const to the temporary object and the temporary will have the same lifetime as the reference (for objects on the stack), so until the end of the scope:
> const QString &filename = tempurl.fileName();
> c.f. https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/
> 
> In my mind it kind of makes more sense to use a reference-to-const when the rvalue (tempurl.fileName()) is not a temporary object, because I am saving nothing by using a reference here since the compiler will create the temporary object and hold it until the end of the scope anyway.
> 
> In this particular case, it's probably exactly the same whether the temporary is used to initialize a const non-reference object and then (the temporary) is dropped/gone or the object being initialized is a reference to const to the temporary until the end of the scope...
> 
> job->statResult() and job->url() are different because both of them return a reference to const.

Thanks for the reference :) to some docs about it. So my memory about this being a chance to limit scope of lifetime of that temporary object was wrong then, so it's simply the same scope as the reference variable, not limited to the last line where the variable is actually used, did I get it right from reading that? (perhaps was some limit in some compiler, it's a decade ago that I was hinted to that, by accident remember the occasion, but less the content :) ).
If so, for the rest, seems we align in not using a const ref for the variable type, instead just making it the official variable holding the object/value, not just a reference. No gain with a const reference, and just confusing by being non-standard code.

REPOSITORY
  R241 KIO

REVISION DETAIL
  https://phabricator.kde.org/D25039

To: meven, #frameworks, dfaure
Cc: ahmadsamir, anthonyfieroni, kossebau, kde-frameworks-devel, LeGast00n, GB_2, michaelh, ngraham, bruns
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-frameworks-devel/attachments/20191031/a2f6fcd8/attachment.html>


More information about the Kde-frameworks-devel mailing list