简体   繁体   English

为什么 std::rand() 的返回类型不是 unsigned int?

[英]Why is the return type of std::rand() not an unsigned int?

std::rand said, std::rand说,

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.

相关问题 std :: vector :: size返回int类型或unsigned int - std::vector::size return an int type or unsigned int <windows>为什么 std::thread::native_handle 返回 'long long unsigned int' 类型的值而不是 void*(又名 HANDLE)?</windows> - <Windows>Why does std::thread::native_handle return a value of type 'long long unsigned int' instead of void* (a.k.a. HANDLE)? 为什么“ unsigned int” +“ unsigned int”返回“ unsigned int”? - Why does “unsigned int” + “unsigned int” return an “unsigned int”? 为什么类型 unsigned 使 std::variant<int64_t,uint64_t> 模棱两可的构造? - Why type unsigned makes std::variant<int64_t,uint64_t> ambiguous to construct? 为什么 (-i) 的类型,其中 i 是 unsigned int,仍然是 unsigned int? - Why is the type of (-i), where i is unsigned int, still unsigned int? 将unsigned int与std :: string :: size_type进行比较是否安全 - Is it safe to compare an unsigned int with a std::string::size_type std :: abs用于“unsigned long long int”数据类型 - std::abs for “unsigned long long int” data type std::byte 位运算符 |、&amp;、^、~:为什么要转换为无符号整数? - std::byte bitwise operators |, &, ^, ~: why cast to unsigned int? 为什么std :: assoc_laguerre的第二个参数是unsigned int? - Why is the second parameter of std::assoc_laguerre an unsigned int? 为什么int(a)是一个表达式而int(unsigned(a))在下面的例子中是一个type-id? - Why is int(a) an expression and int(unsigned(a)) a type-id in the example below?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM