简体   繁体   English

为什么不是`std::uniform_int_distribution<uint8_t> ` 和 `std::uniform_int_distribution<int8_t> ` 允许吗?

[英]Why Aren't `std::uniform_int_distribution<uint8_t>` and `std::uniform_int_distribution<int8_t>` Allowed?

As the documentation says :正如文档所说

The effect is undefined if this is not one of short , int , long , long long , unsigned short , unsigned int , unsigned long , or unsigned long long .如果这不是shortintlonglong longunsigned shortunsigned intunsigned longunsigned long long之一,则效果未定义。

If I don't care about the range, I can just mask off the bits of a larger type to generate random numbers.如果我不关心范围,我可以屏蔽较大类型的位来生成随机数。 If not, it's more complex.如果没有,那就更复杂了。 Why aren't the byte types provided just by default?为什么不默认提供字节类型?

There is a library working group unresolved [1] issue on this uniform_int_distribution<unsigned char> should be permitted and it says, amongst other things:有一个图书馆工作组未解决的[1]问题关于这个uniform_int_distribution<unsigned char> 应该被允许,它说,除其他外:

I am not aware of anything in <random> that works with 16-bit integers but fails with 8-bit integers, so I suspect that IntType and UIntType could simply be extended to permit the char family.我不知道<random>中有任何可以处理 16 位整数但不能处理 8 位整数的内容,所以我怀疑可以简单地扩展IntTypeUIntType以允许 char 系列。 Alternatively, this change could be limited to uniform_int_distribution alone, where it is definitely safe.或者,此更改可能仅限于uniform_int_distribution,这绝对是安全的。 A <random> expert should decide which change is best. <random>专家应该决定哪种更改是最好的。

The proposed resolution is to change the restriction to allow standard integer types:提议的解决方案是更改限制以允许标准整数类型:

that has a template type parameter named IntType is undefined unless the corresponding template argument is cv-unqualified and is aa standard integer type (3.9.1 [basic.fundamental]具有名为IntType的模板类型参数是未定义的,除非相应的模板参数是 cv-unqualified 并且是标准整数类型 (3.9.1 [basic.fundamental]

and:和:

that has a template type parameter named UIntType is undefined unless the corresponding template argument is cv-unqualified and is a standard unsigned integer type (3.9.1 [basic.fundamental])具有名为UIntType的模板类型参数是未定义的,除非相应的模板参数是 cv-unqualified 并且是标准的无符号整数类型 (3.9.1 [basic.fundamental])

This gets you unsigned / signed char although not uint8_t or int8_t but they are likely equivalent.虽然不是uint8_tint8_t,但它们可能是等效的,这会让您获得无符号/有符号字符 Extended integral types were exluded to simplify the wording and maximize consensus:扩展的整数类型被排除在外,以简化措辞并最大限度地达成共识:

This also excludes extended integral types and wide char types, which seem like nice-to-haves at best.这也不包括扩展的整数类型和宽字符类型,它们充其量似乎是最好的。 I have no objection to supporting any of those types;我不反对支持任何这些类型; I just picked this to simplify the wording and hopefully maximize consensus.我只是选择这个来简化措辞并希望最大限度地达成共识。

Note, this excludes char since it is implementation defined whether char is signed or not.请注意,这不包括char因为它是实现定义的,无论char是否有符号。

Note this topic was also brought up in the std-discussion list .请注意,此主题也在std-discussion list 中提出

Jonathan Wakely notes this proposal is controversial and commented that his notes from the last discussion include the following: Jonathan Wakely 指出这个提议是有争议的,并评论说他在上次讨论中的笔记包括以下内容:

that it was very definitely intentional that single byte integers are not supported, not an accidental omission, and so we should be careful about just changing that without consulting the designers of the C++11不支持单字节整数绝对是故意的,不是偶然的遗漏,所以我们应该小心改变它而不咨询 C++11 的设计者

He suggests adding a member to random_device to provide single bytes, which is seems like a reasonable alternative.他建议向random_device添加一个成员以提供单个字节,这似乎是一个合理的选择。


[1] The issue has been closed as " Not A Defect ", meaning that it will not be resolved as a defect report. [1] 该问题已作为“非缺陷”关闭,这意味着它不会作为缺陷报告解决。 Instead a formal proposal for the change will be required.相反,将需要正式的变更提案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM