[Development] Another integer typedef OR how to prepare for 64-bit in Qt 5
Thiago Macieira
thiago.macieira at intel.com
Tue Dec 4 18:02:52 CET 2018
On Tuesday, 4 December 2018 08:35:58 PST Bernhard Lindner wrote:
> > The literal "1" is int, not size_t or long, so i,'s far easier to write
> > int
> > code than size_t.
>
> You think this is a strong argument?
No, it's a weak argument, but an argument nonetheless. If you wrote
n + 1
and n is size_t, then you have a type mismatch. Whether a compiler warns about
that or not is unpredictable, so you will need to cast the 1, or at least make
it unsigned:
n + 1U
> > In any case, Qt will not use an unsigned type.
>
> So qsizetype will be signed in Qt6?
Yes, as it is in Qt 5. And as is the current recommendation of the C++
standard committee: use unsigned *only* where you need modulo-2 overflow
arithmetic, otherwise signed.
Also note how the following expression:
if (i - 1 < size())
Can have surprising results if i and size() are unsigned.
> What a pitty. I assumed the non-compliant int usage would be some legacy
> burden and hoped it would be fixed at the earliest opportunity.
Compliant to what? Nothing in the standard requires use of a given type or
another. If anything, we're going to be more compliant to the committee wishes
than the standard containers themselves.
> Anyway the usage of qsizetype would be an improvment.
Indeed, that's why I am pushing for it.
Note also how the "n + 1" code with n being a qsizetype would not warn.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Development
mailing list