0

So going through the QFile docs, I found QFile::size() returns a qint64.

It would make sense (to me atleast) if QFile::size() returned an unsigned integer i.e. quint64 as file sizes range from 0 bytes and upwards. QFile::size() reimplements some methods from parent classes, i.e. QFileDevice::size() and it again from QIODevice::size(). In non of these implementations any mention is made of negative numbers i.e. -1, etc.

The base implementation in QIODevice::size() has a description:

For open random-access devices, this function returns the size of the device. For open sequential devices, bytesAvailable() is returned.

If the device is closed, the size returned will not reflect the actual size of the device.

In QIODevice::bytesAvailable() which dictates the return type of QIODevice::size() also makes no mention of -1 return values, however its return type is also qint64.

Is this an design decision for Qt that has a use for negative numbers if file sizes, and be extension their parent class(es) too i.e.

CybeX
  • 2,060
  • 3
  • 48
  • 115
  • Maybe negative numbers are returned when the device is closed, and a similar thing is done for files? – cigien Aug 19 '20 at 15:03
  • @cigien looking through the docs, I didn't find any mention of it hence my interest in the data type – CybeX Aug 19 '20 at 15:04
  • 1
    its a long-standing debate whether container sizes should be unsinged or not. There are a couple of nasty subtle errors, eg `for (unsigned i = 0; i < c.size()-1;++i)` with an empty container, that can be avoided with a signed size – 463035818_is_not_an_ai Aug 19 '20 at 15:05
  • 1
    Not sure about qt, but returning unsigned from `size` is a historical accident and a source of bugs. In fact, c++20 adds `ssize()` to solve this exact issue, so maybe that's what qt is going for. – cigien Aug 19 '20 at 15:05
  • 2
    FWIW, I'm in the camp that says size functions return signed integers. That way you don't have to worry about unsigned arithmetic. If you subtract to file sizes, and the first happens to be bigger then the second then instead of getting a negative number you get a very large number instead. – NathanOliver Aug 19 '20 at 15:06
  • 1
    Qt in general uses signed ints everywhere. – Waqar Aug 19 '20 at 15:06
  • consider that banning unsigned altogether is also a way to go (not a nice one, but others disagree), I think eventually it is purely opinion-based – 463035818_is_not_an_ai Aug 19 '20 at 15:07

0 Answers0