[英]Why is the return type of std::rand() not an unsigned int?
int rand();
Returns a pseudo-random integral value between 0 and RAND_MAX (0 and RAND_MAX included).返回一个介于 0 和 RAND_MAX(包括 0 和 RAND_MAX)之间的伪随机整数值。
Since it is guaranteed that a non-negative integer will be returned, why the return type is signed?既然保证返回一个非负的integer,为什么返回类型是有符号的呢?
I am not talking about if we should use it here.我不是在谈论我们是否应该在这里使用它。 Is it a historical issue or some bad design?这是一个历史问题还是一些糟糕的设计?
There is much debate about unsigned
.关于unsigned
有很多争论。 Without going too much into subjective territory, consider the following: What matters is not whether the value returned from rand()
cannot be negative.在不过多探讨主观领域的情况下,请考虑以下内容:重要的不是从rand()
返回的值是否不能为负数。 What matters is that rand()
returns a value of a certain type and that type determines what you can do with that value.重要的是rand()
返回特定类型的值,并且该类型决定了您可以对该值执行的操作。 rand()
never returns a negative value, but does it make sense to apply operations on the value that make the value negative? rand()
从不返回负值,但是对使值变为负值的值应用操作是否有意义? Certainly yes.当然可以。 For example you might want to do:例如你可能想做:
auto x = rand() % 6 - 3;
If rand()
would return an unsigned
then this would lead to confusing bugs in code that looks ok.如果rand()
将返回一个unsigned
的,那么这将导致看起来不错的代码中出现令人困惑的错误。
Using unsigned
for example for indices is a different story.例如使用unsigned
索引是另一回事。 An index is always positive.指数总是正的。 If you want to apply operations to it that turn the value negative then it is not an index anymore.如果你想对它应用将值变为负值的操作,那么它就不再是索引了。 rand() % 6 -3
on the other hand is a random number, be it positive or not.另一方面, rand() % 6 -3
是一个随机数,无论是否为正数。
A type is more than the range of values it can represent.类型超出了它可以表示的值的范围。 Signed and unsigned integers have different semantics.有符号和无符号整数具有不同的语义。
Note that C++20 introduced std::ssize
.请注意,C++20 引入了std::ssize
。 It's the size of a container, it can only be positive.一个容器那么大,只能是正数。 Nevertheless it is signed.尽管如此,它还是被签署了。 That's one example for positive values that are signed merely to allow signed arithmetics.这是一个正值的例子,它被签名只是为了允许有符号的算术。 Also it was not an option to change std::size
to return a signed, because that would break existing code.也不是更改std::size
以返回签名的选项,因为这会破坏现有代码。
And as a sidenote consider that Java has no unsigned integer type at all, because unsigned arithmetics were considered too confusing.作为旁注,请考虑 Java 根本没有无符号 integer 类型,因为无符号算术被认为太混乱了。
Stroustrup writes in "The C++ Programming Language: 6.2.4 Integer Types": *The unsigned integer types are ideal for uses that treat storage as a bit array. Stroustrup 在“C++ 编程语言:6.2.4 Integer 类型”中写道:*无符号integer 类型非常适合将存储视为位数组的用途。 Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea.使用unsigned而不是int来获得更多一位来表示正整数几乎不是一个好主意。 Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules.通过声明无符号变量来确保某些值是正数的尝试通常会被隐式转换规则打败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.