PDA

View Full Version : Qwt 6: moving to size_t (unsigned) integer type for sizes and indexes was unfortunate



philw
4th October 2015, 22:22
There was so much great software engineering cleanup in Qt 6 and 6.1. Really quite excellent.

But one thing I was sorry to see was the move to unsigned integers (size_t) for size and index values, for example in these analogous methods:



Qwt5: int QwtPlotCurve::dataSize() const;
Qwt6: size_t QwtSeriesStore<T>::dataSize() const;


I regard size_t as an historical mistake. Any value to which subtraction would be applied really should be signed. Note that making an integer unsigned of course doesn't protect against it being assigned what is (or should be) a negative value -- it only guarantees that if that happens, the result is a sometimes difficult-to-detect disaster.

It's not uncommon to assign (-1) to an undefined index variable. That can be recognized regardless of the size of the array to which it would be applied -- BUT ONLY if we sacrifice one little single bit to allow the representation of negative numbers. Note that a <size_t> value is NEVER < 0 (even if it is) -- a very likely coding mistake, resulting in invalid memory references -- or an infinite loop -- i.e. disaster.

And when applying dynamic offsets to an index, the result could certainly be negative, and the only way to detect that is to get your hands on the size of the array to which it would be applied, and using a non-intuitive expression, (>= size), for this condition.

Qwt5's choice of a (signed) int, as is used in Qt proper, for these values was better. $0.02 (and then some).

Uwe
5th October 2015, 07:03
Your observation is not 100% correct: size_t is inherited from Qwt5 ( AFAIR even earlier, see the history of QwtData ) invented at a time when Qt containers where also using unsigned. All what has been done in Qwt6 is to fix the inconsistent API of a wrapping method.
When Qt development came up with a new template library it was not possible to simply adjust Qwt - remember Qwt5 supports Qt3/Qt4.

But I agree, that using integers has become standard since Qt4 and it could be changed in Qwt as well.

Uwe