A group of variadic macros

前端 未结 2 2051
暗喜
暗喜 2021-01-19 03:56

I would like to have a group of variable number of arguments passed into a macro. I have following macros which is incorrect:

#define M_NARGS(...) M_NARGS_(_         


        
2条回答
  •  伪装坚强ぢ
    2021-01-19 04:19

    I'm not sure whether this is what you are looking for, but the parenthesised fruit and animal groups are not resolved. You can "flatten" them with your M_IDmacro, e.g.:

    #define M_ID(...) __VA_ARGS__
    
    #define FRUITS M_ID(apple, banana, cherry)
    #define ANIMALS M_ID(dog, monkey)
    
    #define ZOO_BLOCK(NAME, FRTS, ANMLS) struct NAME##Block {   \
      M_FOR_EACH(DEFINE_FRUITS, FRTS)                           \
      M_FOR_EACH(DEFINE_ANIMALS, ANMLS)                          \
    }
    
    #define DEFINE_FRUITS(F) Fruit F;
    #define DEFINE_ANIMALS(F) Animal F;
    
    ZOO_BLOCK(MyZoo, FRUITS, ANIMALS);
    

    This, together with correcting a minor typo in DEFINE_ANIMAL/S yields:

    struct MyZooBlock { 
        Fruit apple;
        Fruit banana;
        Fruit cherry;
        Animal dog;
        Animal monkey;
    };
    

提交回复
热议问题