Is this warning related to enum class size wrong?

前端 未结 2 1970
执念已碎
执念已碎 2021-01-19 02:55

Warning:

src/BoardRep.h:49:12: warning: ‘BoardRep::BoardRep::Row::::a’ 
is too small to hold all values of ‘enum class BoardRep::Piec         


        
相关标签:
2条回答
  • 2021-01-19 03:27

    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.

    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 STANDARD

    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.

    7.2 Enumeration declarations [dcl.enum]

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


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

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

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


    DETAILS

    enum class E : unsigned char { A, B, C };
    
    E x = static_cast<E> (10);
    

    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.

    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!?
    
    0 讨论(0)
  • 2021-01-19 03:34

    According to the C++ Standard

    8 For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type.

    So the values of your enumeration are in the range

    std::numeric_limits<unsigned char>::min() - std::numeric_limits<unsigned char>::max()
    

    Bit field a defined as

    Piece a:2;
    

    can not hold all values of the enumeration.

    If you would define an unscoped enumeration without a fixed underlying type then the range of its values would be

    0 - 2
    
    0 讨论(0)
提交回复
热议问题