[Development] QStringLiteral vs QLatin1String , foreach vs for range

Olivier Goffart olivier at woboq.com
Sun Jan 17 19:31:54 CET 2016


On Sonntag, 17. Januar 2016 19:13:44 CET Gunnar Roth wrote:
> Hi,
> I saw quite some changes like https://codereview.qt-project.org/#/c/145961/
> Replace QStringLiteral with QLatin1String in QFileSelector. I also read
> about the problem of QStringLiteral concerning plugins, but what is the idea
> behind these changes ?
> 
> There is also a lot of replacing foreach with range for loops? What is the
> idea behind this? Any links to read?

About QStringLiteral: 
As explained here: https://woboq.com/blog/qstringliteral.html
QStringLiteral can avoid malloc and conversion to QString. But there is an 
operator==(const QString&,QLatin1String) which also don't do allocations or 
conversion.  In that case QLatin1String is slightly better because it takes 
twice as less space in the binary (UTF-16 vs. ASCII)


As for foreach and range for loops: the foreach macro does a bit more by doing 
a copy of the container and call the non const begin and end functions. That's 
a very small costs for qt containers (a few atomic operations) but it saves 
code space.

All of this is probably negligible, but it helps reducing the size of the 
library.

-- 
Olivier 

Woboq - Qt services and support - https://woboq.com - https://code.woboq.org



More information about the Development mailing list