Why does Qt use a signed integer type for its container classes?

后端 未结 2 1305
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 13:33

The question is clear.

I wonder why they even thought this would be handy, as clearly negative indices are unusable in the containers that would be used with them (s

2条回答
  •  甜味超标
    2021-01-17 13:43

    Because, realistically, you usually want to perform arithmetic on indices, which means that you might want to create temporaries that are negative. This is clearly painful when the underlying indexing type is unsigned.

    The only appropriate time to use unsigned numbers is with modulus arithmetic. Using "unsgined" as some kind of contract specifier "a number in the range [0..." is just clumsy, and too coarse to be useful.

    Consider: What type should I use to represent the idea that the number should be a positive integer between 1 and 10? Why is 0...2^x a more special range?

提交回复
热议问题