Qt6 C++ compilation changes
-
QList's (and hence QVector's) size type is changed from int to qsizetype. Together with the size type, all relevant methods' signatures are updated to use qsizetype. This allows QList to hold more than 2^31 items on 64 bit platforms.
-
@JonB said in Qt6 C++ compilation changes:
std::max(function_returning_qsizetype(), 10LL)
Using
10LL
is not portable. I don't really remember which type had this problem, but I once ran into a problem with these integer literals that did not have a solution for Windows, Linux, and macOS (yet). Starting from C++23 you haveZ
available which will be actually be portable for this use case (https://en.cppreference.com/w/cpp/language/integer_literal). -
@SimonSchroeder
What a mess trying to find a simple literal of the same type as something as basic as whatQList::count()
returns!For my part I only have to support gcc.
I shan't be writing
for (qsizetype i = 0LL; i < list.count()
, I will be sticking withint
. So one possibility would be to convert/cast theqsizetype
toint
for thestd::max()
so I can use a plain literal. But I think I will abandonstd::max()
forqMax()
to avoid this ugliness. -
I shan't be writing
for (qsizetype i = 0LL; i < list.count()
, I will be sticking withint
.It might fail for very large lists ;-) But
for (qsizetype i = 0; i < list.count()
should work just fine.As for max, this is also an option if you like to type a lot:
std::max(jsonArray.count(), static_cast<qsizetype>(10)).
-
@sierdzio said in Qt6 C++ compilation changes:
It might fail for very large lists
Absolutely! But as you can see the kind of size I have in mind is
10
. That is a long way away from2 ^ 63
:)static_cast<qsizetype>(10)
Of course! But I am simply not prepared to write to write that when comparing against
10
! -
@JonB This change matches the one in C++. I used to have loops like
std::vector< something > vec; for ( unsigned idx = 0; idx < vec.size(); ++idx ) { }
In new C++
std::vector< something > vec; for ( size_t idx = 0; idx < vec.size(); ++idx ) { }
If you do not like the casting, I guess you can replace your loop with
for ( auto const & element : your list ) { }
Simpler.
-
@JoeCFD
I object to usingsize_t
(should really bessize_t
which is whatqsizetype
is) as my "default" loop counter type when I have thousands of existing occurrences usingint
! (Having said that, just to be clear, the change in type/size does not prevent me from usingint
even now, so I don't have to change everything. It is something likestd::max()
which is affected/falls foul of the new definition.)Your last case is more or less what I use when iterating and I do not need the index inside the loop. But the integral counter is required/easier when you do need to know the index value.
-
@JonB from here
size(C++17)
ssize(C++20)
Something new for me. Good to know.https://en.cppreference.com/w/cpp/iterator/size
Recently I built a project on Windows with C++ 14. size_t is needed to replace int for std::vector in the build warnings.
when you need an index
int index{}; (if you prefer int) for ( auto const & element : your list ) { do something with index index++; }
-
@JoeCFD
Sorry if you think I am being difficult, but I'm not going to write a "for-element-in-list" loop and then maintain a separate index counter! One or the other!Besides I was talking about existing code already written. But as a I say I can still use an
int
loop counter at Qt6, at least with gcc, without warning. It's just something likestd::max()
which barfs.We can debate further if you wish, but I do get that Qt etc. need to change to
long long
for size/count/array-indexing etc. I just don't like how messy it is making it (forstd::max()
at least) to pass a simple, numeric, non-qualified literal.