[Development] QtCS 2017 QtCore sessions

André Somers andre at familiesomers.nl
Wed Nov 1 21:15:11 CET 2017



Op 01/11/2017 om 21:00 schreef Thiago Macieira:
> On quarta-feira, 1 de novembro de 2017 12:03:27 PDT André Somers wrote:
>> Using signed for size types is crucial because the API expects to be able to
>>> count backwards from the end and needs to report failure in other
>>> situations. So unsigned is simply ruled out.
>> I think we're stuck with that API indeed, but _if_ we had the freedom to
>> re-design it, it would not be my choice to do it this way. I'd sooner
>> choose for an explicit flag for the first case, and something like
>> std::optional as a return value to handle the error-reporting case. I'd
>> find that more explicit that using negative indices. However, I guess we
>> cannot possibly break API that badly in Qt 6, so doing something like
>> that is out of the question.
> That doesn't allow implementing indexOf() that searches from a position from 
> the end. It's not just failure modes.
>
> So, no, even with a redesign we'd stick with signed.
>
That doesn't make sense. Of course it allows for that.

You'd give indexOf a flag like Qt::searchTowardsBeginning (default being
Qt::searchTowardsEnd) to determine the direction to search in. Or do you
mean that you want the result also to be counted from the end? If all
API's take the index counted from the begining there would not be much
use for that. You either calculate it yourself by substracting the
result from length(), or you add another flag and do something like
std::optional<qsize> indexOf(needle, startIndex,
Qt::searchTowardsBeginning | Qt::asCountedFromEnd) .

Personally, after many years of using the Qt APIs, I do not find the
APIs taking negative numbers as an index intuitive.

André





More information about the Development mailing list