question regarding QLatin1String and QLatin1Char

Rolf Eike Beer kde at opensource.sf-tec.de
Tue Aug 7 07:02:59 UTC 2012


Am Montag, 6. August 2012, 17:43:28 schrieb Evan Teran:
> I've been working on a fairly large improvement to kcalc. Specifically a
> much improved knumber library which will eventually bring arbitrary
> precision trig functions :-).
> 
> Anyway, looking throughout the code, I see a LOT of usage of QLatin1String
> and QLatin1Char and I'm not 100% sure what the benefit is.
> 
> From the documentation is looks as though it's primary usage is to be able
> to specify string constants without needed to create a full QString object.
> here's my question, if that constant is going to become a QString anyway,
> is there any benefit? For example:
> 
> // does this make sense?
> QString func() {
>     return QLatin1String("Hello world");
> }
> 
> The documentation is even more vague about the benefits of QLatin1Char, it
> says it used to avoid codec stuff, which I highly doubt is being used in
> kcalc. So can I expect there to be any benefit?
> 
> Unfortunately while it is used in a lot of places, the usage isn't 100%
> consistant, based on the fact that things seem to work, I'm not sure what
> improvement it is offering.

If you compile with QT_NO_CAST_FROM_ASCII you are not allowed to create a 
QString directly from a char*. This class is basically the fix for it. And 
since it is named QLatin1String is always assumes the input is Latin1 encoded. 
By this it is possible to avoid a lot of code to detect what to do with it 
when creating the QString object. Since the charset is knows there is no need 
for detection or switching between cases anymore.

Eike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-utils-devel/attachments/20120807/fda8a3e8/attachment.sig>


More information about the Kde-utils-devel mailing list