typedef enum使用出错 RRS feed

  • 问题

  • 在使用typedef enum时,遇到了如果枚举值为64位则会被截断


    typedef enum siphash_constants
        SIPHASHM_0 = 0x736f6d6570736575UL,
        SIPHASHM_1 = 0x646f72616e646f6dUL,
        SIPHASHM_2 = 0x6c7967656e657261UL,
        SIPHASHM_3 = 0x7465646279746573UL

    } siphash_constants_t;


    SIPHASHM_0 = 0x70736575  (其它的类似)


    warning C4309: “初始化”: 截断常量值

    请问各位,有解决方案吗?(要求用typedef enum)

    2021年8月26日 4:31


  • typedef enum siphash_constants
        SIPHASHM_00 = 0x736f6d65UL,
        SIPHASHM_01 = 0x70736575UL,
        SIPHASHM_10 = 0x646f7261UL,
        SIPHASHM_11 = 0x6e646f6dUL,
        SIPHASHM_20 = 0x6c796765UL,
        SIPHASHM_21 = 0x6e657261UL,
        SIPHASHM_30 = 0x74656462UL,
        SIPHASHM_31 = 0x79746573UL
    } siphash_constants_t;
    2021年8月26日 5:01
  • typedef enum siphash_constants在CYGWIN64下使用64位数据定义没问题,但移植到VS2019下后才发现有问题的,现改为

    typedef enum siphash_constants : unsigned long long后编译正常了。


    2021年8月26日 7:46
  • enums类型为POD,Plain old data structure,缩写为POD,是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中。

    A POD is a type (including classes) where the C++ compiler guarantees that there will be no "magic" going on in the structure: for example hidden pointers to vtables, offsets that get applied to the address when it is cast to other types (at least if the target's POD too), constructors, or destructors. Roughly speaking, a type is a POD when the only things in it are built-in types and combinations of them. The result is something that "acts like" a C type.

    Less informally:
    •int, char, wchar_t, bool, float, double are PODs, as are long/short and signed/unsigned versions of them.
    •pointers (including pointer-to-function and pointer-to-member) are PODs,
    enums are PODs
    •a const or volatile POD is a POD.
    •a class, struct or union of PODs is a POD provided that all members are public, and it has no base class and no constructors, destructors, or virtual methods. Static members don't stop something being a POD under this rule.
    •Wikipedia is wrong to say that a POD cannot have members of type pointer-to-member. Or rather, it's correct for the C++98 wording, but TC1 made explicit that pointers-to-member are POD.

    2021年8月27日 6:03