Why does Qt use a signed integer type for its cont

2020-04-02 07:30发布

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 (see for example QList's docs).

I thought they wanted to allow that for some crazy form of indexing, but it seems unsupported?

It also generates a ton of (correct) compiler warnings about casting to and comparing of signed/unsigned types (on MSVC).

It just seems incompatible with the STL by design for some reason...

标签: c++ qt qt4
2条回答
▲ chillily
2楼-- · 2020-04-02 07:51

Although I am deeply sympathetic to Chris's line of reasoning, I will disagree here (at least in part, I am playing devil's advocate). There is nothing wrong with using unsigned types for sizes, and it can even be beneficial in some circumstances.

Chris's justification for signed size types is that they are naturally used as array indices, and you may want to do arithmetic on array indices, and that arithmetic may create temporary values that are negative.

That's fine, and unsigned arithmetic introduces no problem in doing so, as long as you make sure to interpret your values correctly when you do comparisons. Because the overflow behavior of unsigned integers is fully specified, temporary overflows into the negative range (or into huge positive numbers) do not introduce any error as long as they are corrected before a comparison is performed.

Sometimes, the overflow behavior is even desirable, as the overflow behavior of unsigned arithmetic makes certain range checks expressible as a single comparison that would require two comparisons otherwise. If I want to check if x is in the range [a,b] and all the values are unsigned, I can simply do:

if (x - a < b - a) {
}

That doesn't work with signed variables; such range checks are pretty common with sizes and array offsets.

I mentioned before that a benefit is that overflow arithmetic has defined results. If your index arithmetic overflows a signed type, the behavior is implementation defined; there is no way to make your program portable. Use an unsigned type and this problem goes away. Admittedly this only applies to huge offsets, but it is a concern for some uses.

Basically, the objections to unsigned types are frequently overstated. The real problem is that most programmers don't really think about the exact semantics of the code they write, and for small integer values, signed types behave more nearly in line with their intuition. However, data sizes grow pretty fast. When we deal with buffers or databases, we're frequently way outside of the range of "small", and signed overflow is far more problematic to handle correctly than is unsigned overflow. The solution is not "don't use unsigned types", it is "think carefully about the code you are writing, and make sure you understand it".

查看更多
干净又极端
3楼-- · 2020-04-02 07:59

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?

查看更多
登录 后发表回答