本文介绍了样式问题:是否始终使用有符号整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经被告知,如果某个积分值永远不会有负值的价值,那么使用无符号类型是一种很好的风格:它是

信息丰富,自我记录,你不会浪费一半的价值

范围你永远不会使用的价值。


我同意这个,并开始总是使用'​​'unsigned''每当

负值没有任何意义。我这么做了好几年了。


然而,我慢慢改变了主意:这样做经常导致更多的问题而不是它的价值。一个很好的例子:


如果你有一个图像类,它的宽度和高度属性可以

*永远*得到负值。他们永远是积极的。因此,对于宽度和高度,使用''unsigned''是有意义的,不是吗?什么

问题可能会产生什么?


好​​吧,假设您在屏幕上绘制图像,按像素

坐标,这些图像可以部分(或完全)在屏幕外面。例如,要绘制的图像的左边缘坐标可能具有负x值(例如,在坐标< -20,50>处绘制100x100图像

)。由于坐标已签名且图像的尺寸未签名,因此可能导致签名未签名

混合。例如:


if(x - width / 2< 1)...


其中''x''是有符号整数,给出*不同的*结果,取决于

''width''是有符号还是无符号,具有某些值的那些

变量(例如x = 2和宽度= 10)。目的是在这里将

''width''视为有符号值,但如果不是,那么比较将会b / b
故障(没有明确表示'' width''到有符号值)。这个

可能完全没有引起注意,因为编译器甚至可能没有给出任何警告(例如gcc没有)
$

因此在某些时候我开始*总是*使用有符号整数,除非

有一个很好的理由不这样做。 (当然这有时会导致

小烦恼,因为STL容器会返回一个无符号值,因为它们的大小()函数只有
,但这通常不是一个大问题。)


听到关于这个主题的其他意见会很有意思。

I was once taught that if some integral value can never have negative
values, it''s a good style to use an ''unsigned'' type for that: It''s
informative, self-documenting, and you are not wasting half of the value
range for values which you will never be using.

I agreed with this, and started to always use ''unsigned'' whenever
negative values wouldn''t make any sense. I did this for years.

However, I slowly changed my mind: Doing this often causes more
problems than it''s worth. A good example:

If you have an image class, its width and height properties can
*never* get negative values. They will always be positive. So it makes
sense to use ''unsigned'' for the width and height, doesn''t it? What
problems could that ever create?

Well, assume that you are drawing images on screen, by pixel
coordinates, and these images can be partially (or completely) outside
the screen. For example, the left edge coordinate of the image to be
drawn might have a negative x value (for example drawing a 100x100 image
at coordinates <-20, 50>). Since the coordinates are signed and the
dimensions of the image are unsigned, this may cause signed-unsigned
mixup. For example this:

if(x - width/2 < 1) ...

where ''x'' is a signed integer, gives *different* results depending on
whether ''width'' is signed or unsigned, with certain values of those
variables (for example x=2 and width=10). The intention is to treat
''width'' here as a signed value, but if it isn''t, the comparison will
malfunction (without explicitly casting ''width'' to a signed value). This
may well go completely unnoticed because compilers might not even give
any warning (for example gcc doesn''t).

Thus at some point I started to *always* use signed integers unless
there was a very good reason not to. (Of course this sometimes causes
small annoyances because STL containers return an unsigned value for
their size() functions, but that''s usually not a big problem.)

It would be interesting to hear other opinions on this subject.

推荐答案



是的,我有类似的经历/想法。


有些人回来后我认为这是个好主意使用无符号整数。

我不认为我在任何地方都读到这是一个好习惯,作为一个多余的时间程序员,我从未参加过C ++课程;它只是看起来''显而易见''

使用unsigned作为永远不会消极的值会更安全。


所以我尝试使用无符号整数仿真项目。但是我很快就开始发现我收到了关于unsigned /

签名转换的编译器警告。当我开始尝试将更多变量更改为

unsigned以停止警告时,我刚开始收到更多警告。


我很快就抛弃了整个事情它的基础是比它的价值更麻烦(但也有点愧疚)。


我认为问题的一部分可能是那个我试图改造现有的代码,而不是从一开始编码就可以使用

unsigned。然而,上面的Juha给出的例子让我觉得这个

不仅仅是改装的问题。似乎有使用unsigned int的情况下使用unsigned int是非常危险的,特别是在编译器没有产生警告的情况下(我刚刚确认)在我自己的

gcc设置,有这样的场景)。


而不是使用无符号,我自由地使用断言,并看到了

签名/未签名转换的危险,我认为这是正确的方法。


Chris Gordon-Smith




哇,这当然令人大开眼界。我假设在这种情况下

这两个值都会被提升为更大的类型(签名长),这可以准确地表示两个值(signed和unsigned int)

但显然不是。


我认为这是标准中的缺陷,因为它允许

正确的操作是取小于int的类型:


#include< iostream>


int main()

{

unsigned short width = 10;

short x = 2;

std :: cout<< (x - 宽度);

返回0;

}


-

Erik Wikstr ?? m

Wow, that was certainly an eye-opener. I had assumed that in this case
both values would be promoted to some larger type (signed long) which
could accurately represent both values (the signed and the unsigned int)
but apparently not.

This is a defect in the standard in my opinion since it allows the
correct action to be taken for types smaller than int:

#include <iostream>

int main()
{
unsigned short width = 10;
short x = 2;
std::cout << (x - width);
return 0;
}

--
Erik Wikstr??m




哇,这当然令人大开眼界。我假设在这种情况下

这两个值都会被提升为更大的类型(签名长),这可以准确地表示两个值(signed和unsigned int)

但显然不是。


我认为这是标准中的缺陷,因为它允许

正确的操作是取小于int的类型:


#include< iostream>


int main()

{

unsigned short width = 10;

short x = 2;

std :: cout<< (x - 宽度);

返回0;

}


Wow, that was certainly an eye-opener. I had assumed that in this case
both values would be promoted to some larger type (signed long) which
could accurately represent both values (the signed and the unsigned int)
but apparently not.

This is a defect in the standard in my opinion since it allows the
correct action to be taken for types smaller than int:

#include <iostream>

int main()
{
unsigned short width = 10;
short x = 2;
std::cout << (x - width);
return 0;
}



是-8(gcc 3.4.5 / adam riese)......

is -8 (gcc 3.4.5/adam riese)...


这篇关于样式问题:是否始终使用有符号整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:03