为什么Qt对其容器类使用带符号的整数类型? [英] Why does Qt use a signed integer type for its container classes?

查看:44
本文介绍了为什么Qt对其容器类使用带符号的整数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很明显.

我想知道为什么他们甚至认为这很方便,因为显然负索引在与它们一起使用的容器中不可用(例如,请参见

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?

它还会生成大量(正确的)编译器警告,有关强制转换和比较有符号/无符号类型(在MSVC上).

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

出于某种原因,它似乎与设计中的STL不兼容...

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

推荐答案

因为,实际上,您通常希望对索引执行算术运算,这意味着您可能希望创建负数的临时变量.当基础索引类型为无符号时,这显然很痛苦.

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.

使用无符号数字的唯一合适时间是使用模数算法.使用"unsgined"作为某种合同说明符,"[0 ...]范围内的数字只是笨拙,而且太粗糙而无用.

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.

考虑:我应该使用哪种类型来表示数字应该是1到10之间的正整数的想法?为什么0 ... 2 ^ x是更特殊的范围?

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?

这篇关于为什么Qt对其容器类使用带符号的整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆