对unsigned int的建议(Gangnam Style版) [英] Advice on unsigned int (Gangnam Style edition)

查看:212
本文介绍了对unsigned int的建议(Gangnam Style版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视频江南风格(我相信你听说过)刚刚超过了youtube上的20亿次观看。事实上,谷歌说他们从来不希望视频大于一个32位整数...这暗示了一个事实,谷歌使用 int 而不是 unsigned



检查他们的样式指南:https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Integer_Types



...他们建议不使用无符号整数类型,并给出一个很好的理由为什么: unsigned 可能是buggy。



这是一个很好的理由,但可以防范。我的问题是:是否是坏的编码练习一般使用 unsigned int

解决方案

Google规则在专业圈子中被广泛接受。问题
是无符号整数类型是破碎的,并且在用于数值时有
意外和不自然的行为;他们
不能很好地作为基本类型。例如,数组
中的索引可能永远不是负数,但是写
abs(i1 - i2)两个指数之间的距离。如果
i1 i2 有未签名的类型,则无效。



作为一般规则,Google风格指南
中的这条特定规则或多或少地对应了该语言设计者的意图。
每当你看到 int 以外的东西,你可以假定一个特殊的
原因。如果是因为范围,它将 long
long long ,甚至 int_least64_t 。使用无符号类型通常是
a信号,你正在处理位,而不是
变量的数值,或者(至少在 unsigned char ),你是
处理原始内存。



对于使用 unsigned :this
不支持,因为
变量不能(或不应该)采用的值总是很多,包括很多正值。 C ++
没有子范围类型,并且 unsigned 的定义方式意味着
它不能真正用作一个。 p>

The video "Gangnam Style" (I'm sure you've heard it) just exceeded 2 billion views on youtube. In fact, Google says that they never expected a video to be greater than a 32-bit integer... which alludes to the fact that Google used int instead of unsigned for their view counter. I think they had to re-write their code a bit to accommodate larger views.

Checking their style guide: https://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Integer_Types

...they advise "don't use an unsigned integer type," and give one good reason why: unsigned could be buggy.

It's a good reason, but could be guarded against. My question is: is it bad coding practice in general to use unsigned int?

解决方案

The Google rule is widely accepted in professional circles. The problem is that the unsigned integral types are sort of broken, and have unexpected and unnatural behavior when used for numeric values; they don't work well as a cardinal type. For example, an index into an array may never be negative, but it makes perfect sense to write abs(i1 - i2) to find the distance between two indices. Which won't work if i1 and i2 have unsigned types.

As a general rule, this particular rule in the Google style guidelines corresponds more or less to what the designers of the language intended. Any time you see something other than int, you can assume a special reason for it. If it is because of the range, it will be long or long long, or even int_least64_t. Using unsigned types is generally a signal that you're dealing with bits, rather than the numeric value of the variable, or (at least in the case of unsigned char) that you're dealing with raw memory.

With regards to the "self-documentation" of using an unsigned: this doesn't hold up, since there are almost always a lot of values that the variable cannot (or should not) take, including many positive ones. C++ doesn't have sub-range types, and the way unsigned is defined means that it cannot really be used as one either.

这篇关于对unsigned int的建议(Gangnam Style版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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