简体   繁体   English

C ++ 11随机数生成器UIntType的矛盾

[英]C++11 random number generator UIntType contradiction

Section 26.5.1.1 paragraph 1 of the C++11 standard (N3242) says: C ++ 11标准(N3242)第26.5.1.1节第1段说:

Throughout this subclause 26.5, the effect of instantiating a template: 在本子条款26.5中,实例化模板的效果:

[...] [...]

f) that has a template type parameter named UIntType is undefined unless the corresponding template argument is cv-unqualified and is one of unsigned short , unsigned int , unsigned long , or unsigned long long . f)具有名为UIntType的模板类型参数是未定义的,除非相应的模板参数是cv-unqualified,并且是unsigned shortunsigned intunsigned longunsigned long long

And it defines the linear congruential generator in 26.5.3.1. 它定义了26.5.3.1中的线性同余生成器。 The definition of the class starts like this: 该类的定义如下所示:

template<class UIntType, UIntType a, UIntType c, UIntType m>
class linear_congruential_engine

minstd_rand0 seems to violate this restriction: minstd_rand0似乎违反了这个限制:

typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
    minstd_rand0;

As it uses uint_fast32_t (which isn't guaranteed to be one of unsigned short , unsigned int , unsigned long , or unsigned long long ) in minstd_rand0 for a template parameter named UIntType , it appears to have undefined effect to #include <random> , or at least to use minstd_rand0 . 因为它使用uint_fast32_t(其不能保证是一个unsigned shortunsigned intunsigned long ,或unsigned long long )在minstd_rand0名为模板参数UIntType ,它似乎有未定义的效果#include <random> ,或者至少使用minstd_rand0 This problem applies to other predefined RNGs as well, and it does not appear to be fixed in C++14. 此问题也适用于其他预定义的RNG,并且在C ++ 14中似乎没有修复。

My questions are: 我的问题是:

  • Is this really a contradiction (or rather an extreme amount of undefined behaviour), or have I missed something? 这真的是一个矛盾(或者说是极端的未定义行为),还是我错过了什么?
  • Has this been mentioned in a defect report? 这是否已在缺陷报告中提及?

Edit: I have noticed that this defect report seems to be related to this problem. 编辑:我注意到这个缺陷报告似乎与这个问题有关。

Yes and no. 是的,不是。 Per section 18.4.1, uint_fast32_t Has to be an alias to an unsigned integer type. 根据第18.4.1节, uint_fast32_t必须是无符号整数类型的别名。 While the only unsigned integer types in C++ are unsigned char, short, int, long, long, long (3.9.1) So the only scenario that the section you mentioned can be a contradiction is that char is somehow 32-bits or wider and and uint_fast32_t is defined to an alias to unsigned char. 虽然C ++中唯一的无符号整数类型是unsigned char,short,int,long,long,long(3.9.1)所以你提到的部分唯一可能是矛盾的情况是char不知何故是32位或更宽的uint_fast32_t被定义为unsigned char的别名。

uint_fast32_t is specified to be the fastest unsigned integer type with width of at least 32 bits. uint_fast32_t被指定为最快的无符号整数类型,宽度至少为32位。

In C++ type system, both character and integer types are integral types, but character types are not integer types (nor vice-versa). 在C ++类型系统中,字符和整数类型都是整数类型,但字符类型不是整数类型(反之亦然)。

Finally, unsigned integer types are exactly the ones enumerated for random generators. 最后,无符号整数类型正是随机生成器枚举的整数类型。

My conclusion is that using uint_fast32_t complies with the standard (unless I missed some part of the standard where it is specifically allowed for uint_fast32_t to be a nonstsndard type, or for the definition of integer type to include nonstandard types). 我的结论是使用uint_fast32_t符合标准(除非我错过了标准的某些部分,其中特别允许uint_fast32_t为非标准类型,或者对于包含非标准类型的整数类型的定义)。

However, I believe the spec should be fixed to try to avoid ambiguous interpretations. 但是,我认为应该修改规范以避免模棱两可的解释。

I'm no expert, but my answer is that yes it is a defect, if you are correct that uint_fast32_t need not be one of those types according to the standard. 我不是专家,但我的回答是,这是一个缺陷,如果你是正确的, uint_fast32_t根据标准不一定是那些类型之一。

The proposal in the NAD issue 2326 you referenced would appear to also resolve this defect. 你引用的NAD问题2326中的提议似乎也解决了这个缺陷。

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

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