为什么不允许`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?

查看:635
本文介绍了为什么不允许`std :: uniform_int_distribution< uint8_t>`和`std :: uniform_int_distribution< int8_t>`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件说明


如果不是 short int long long long 无符号short unsigned int unsigned long unsigned 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?

推荐答案

uniform_int_distribution< unsigned char>应该允许,并且其中包括:


我不知道 < random> 与16位
整数一起使用,但失败的是8位整数,所以我怀疑 IntType
UIntType 可以简单地扩展为允许char系列。
或者,这个变化可以局限于
uniform_int_distribution,在那里它绝对是安全的。 A
< random> 专家应决定哪种变更最好。

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. Alternatively, this change could be limited to uniform_int_distribution alone, where it is definitely safe. A <random> expert should decide which change is best.

建议的解决方案是更改限制以允许标准整数类型:

The proposed resolution is to change the restriction to allow standard integer types:


具有名为<$ c $的模板类型参数c> IntType 是未定义的,除非
对应的模板参数是cv-unqualified,并且是aa
标准整数类型(3.9.1 [basic.fundamental]

that has a template type parameter named IntType is undefined unless the corresponding template argument is cv-unqualified and is a a standard integer type (3.9.1 [basic.fundamental]

和:


c $ c> UIntType 未定义
,除非相应的模板参数是cv-unsqualified并且是
标准无符号整数类型(3.9.1 [basic.fundamental])


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])

虽然不是 uint8_t ,但可以获得

This gets you unsigned / signed char although not uint8_t or int8_t but they are likely equivalent. Extended integral types were exluded to simplify the wording and maximize consensus:


int8_t p>这也排除了扩展的整数类型和宽字符类型,看起来好像是最好的。我不反对支持任何这些类型;

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.

注意,这不包括 char 因为它是实现定义 char 是否已签名。

Note, this excludes char since it is implementation defined whether char is signed or not.

标准讨论列表 a>。

Jonathan Wakely指出这个建议有争议,并评论他上次讨论的笔记包括:

Jonathan Wakely notes this proposal is controversial and commented that his notes from the last discussion include the following:


这是非常明确有意的单字节整数不被支持,而不是意外的遗漏,所以我们应该小心改变,而不咨询C ++ 11的设计师

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

他建议为 random_device 添加一个成员以提供单个字节像一个合理的选择。

He suggests adding a member to random_device to provide single bytes, which is seems like a reasonable alternative.

这篇关于为什么不允许`std :: uniform_int_distribution&lt; uint8_t&gt;`和`std :: uniform_int_distribution&lt; int8_t&gt;`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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