简体   繁体   中英

Generating enum class members with a macro

I want to create an enum with 255 elements as follows:

enum class MyEnum
{
    Item0,
    Item1,

    //....

    Item254,
    Item255
};

Is there any way to generate the members of the enum using a macro instead of actually listing them all?

Well, it seems what the OP is after is BOOST_PP_ENUM_PARAMS . I won't say it's the best solution, but it does work for an enum with values like these:

enum class MyEnum {
    BOOST_PP_ENUM_PARAMS(256, Item)
};

您可能会使用Shell脚本来生成它们...我认为它们不会经常更改

echo Item{0..255}, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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