这个警告是否与枚举类大小有关? [英] Is this warning related to enum class size wrong?

查看:152
本文介绍了这个警告是否与枚举类大小有关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:

src/BoardRep.h:49:12: warning: ‘BoardRep::BoardRep::Row::<anonymous struct>::a’ 
is too small to hold all values of ‘enum class BoardRep::Piece’ 
[enabled by default]
Piece a:2;
        ^

枚举:

enum class Piece: unsigned char {
    EMPTY,
    WHITE,
    BLACK
};

使用:

union Row {
    struct {
        Piece a:2;
        Piece b:2;
        Piece c:2;
        Piece d:2;
        Piece e:2;
        Piece f:2;
        Piece g:2;
        Piece h:2;
    };
    unsigned short raw;
};

使用枚举 GCC,它可能必须截断,但这是因为 enum s不是真正分离整数和预处理器定义。但是枚举类要强得多。如果它不够强到假设ALL Piece 作为整数的值将在0和2之间,那么警告是有意义的。否则,GCC是不必要的挑剔,可能值得邮寄列表说看,这是一个愚蠢的警告

With an enum I'd agree with GCC, it may have to truncate but that's because enums are not really separate from integers and pre-processor definitions. However an enum class is much stronger. If it is not strong enough to assume ALL Piece values taken as integers will be between 0 and 2 inclusive then the warning makes sense. Otherwise GCC is being needlessly picky and it might be worth mailing the list to say "look, this is a silly warning"

您可以在2位数据中存储4个不同的值,我只需要3个不同的值,因此长度为4或更小的任何枚举都应该很好地适合给出的2位(和我的枚举从无符号类型导出(更好的术语?)。如果我有5个以上,我会收到警告。

You can store 4 distinct values in 2 bits of data, I only need 3 distinct values, so any enum of length 4 or less should fit nicely in the 2 bits given (and my enum does "derive" (better term?) from an unsigned type). If I had 5 or more THEN I'd expect a warning.

推荐答案

gcc 是准确的,没有必要撰写邮件到邮件列表,要求他们不太可能出现警告。

The warning issued by gcc is accurate, there's no need to compose a mail to the mailing list asking them to make the warning less likely to appear.

具有 unsigned char 的基本类型的枚举不能由长度 2 的位域表示;即使没有持有此值的枚举。

The standard says that an enumeration with the underlying type of unsigned char cannot be represented by a bitfield of length 2; even if there are no enumerations that holds such value.

标准

即使没有与该值对应的枚举键,枚举的基础值也是有效的,该标准仅说明要存储在枚举中的合法值必须在底层类型内;它没有说明这样的值必须存在于枚举键之间。

The underlying value of an enumeration is valid even if there are no enum-keys corresponding to this value, the standard only says that a legal value to be stored inside an enumeration must fit inside the underlying type; it doesn't state that such value must be present among the enum-keys.


枚举声明 [dcl.enum]


7 ... 可以定义一个枚举值,该枚举值具有未由任何枚举器定义的值。 ...

7 ... It is possible to define an enumeration that has values not defined by any of its enumerators. ...



注意:引用的部分出现在C ++ 11和C ++ 14的草稿中。

Note: the quoted section is present in both C++11, and the draft of C++14.

注意:使用不同术语的措辞可以在 [dcl.enum] p6

Note: wording stating the same thing, but using different terminology, can be found in C++03 under [dcl.enum]p6

注意:<$ c $

Note: the entire [decl.enum]p7 hasn't been included to preserve space in this post.

详细信息

enum class E : unsigned char { A, B, C };

E x = static_cast<E> (10);

上面我们初始化 x 即使枚举类E的枚举声明中没有枚举键 10 c>这仍然是一个有效的构造。

Above we initialize x to store the value 10, even if there's no enumeration-key present in the enum-declaration of enum class E this is still a valid construct.

考虑到上述情况,我们很容易推断出 10 在长度 2 的位字段中,因此 gcc 的警告是准确的...我们可能尝试

With the above in mind we easily deduce that 10 cannot be stored in a bit-field of length 2, so the warning by gcc is nothing but accurate.. we are potentially trying to store values in our bit-field that it cannot represent.

>

EXAMPLE

enum class E : unsigned char { A, B, C };

struct A {
  E value : 2;
};

A val;

val.value = static_cast<E> (10); // OMG, OPS!?

这篇关于这个警告是否与枚举类大小有关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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