std::vector::size()
returns a size_type
which is unsigned and usually the same as size_t
, e.g. it is 8 bytes on 64bit platforms.
In constrast, QVector::size()
returns an int
which is usually 4 bytes even on 64bit platforms, and at that it is signed, which means it can only go half way to 2^32.
Why is that? This seems quite illogical and also technically limiting, and while it is nor very likely that you may ever need more than 2^32 number of elements, the usage of signed int cuts that range in half for no apparent good reason. Perhaps to avoid compiler warnings for people too lazy to declare i
as a uint
rather than an int
who decided that making all containers return a size type that makes no sense is a better solution? The reason could not possibly be that dumb?
unsigned
numbers are values mod2^n
for somen
.Signed numbers are bounded integers.
Using unsigned values as approximations for 'positive integers' runs into the problem that common values are near the edge of the domain where unsigned values behave differently than plain integers.
The advantage is that unsigned approximation reaches higher positive integers, and under/overflow are well defined (if random when looked at as a model of Z).
But really,
ptrdiff_t
would be better thanint
.This has been discussed several times since Qt 3 at least and the QtCore maintainer expressed that a while ago no change would happen until Qt 7 if it ever does.
When the discussion was going on back then, I thought that someone would bring it up on Stack Overflow sooner or later... and probably on several other forums and Q/A, too. Let us try to demystify the situation.
In general you need to understand that there is no better or worse here as
QVector
is not a replacement forstd::vector
. The latter does not do any Copy-On-Write (COW) and that comes with a price. It is meant for a different use case, basically. It is mostly used inside Qt applications and the framework itself, initially for QWidgets in the early times.size_t
has its own issue, too, after all that I will indicate below.Without me interpreting the maintainer to you, I will just quote Thiago directly to carry the message of the official stance on:
io/qfilesystemiterator_unix.cpp
io/qfsfileengine.cpp
io/qiodevice.cpp
That was one email from Thiago and then there is another where you can find some detailed answer:
Going into this topic even further with Thiago's responses, see this: