Why is this nested macro replacement failing?

后端 未结 3 1076
忘了有多久
忘了有多久 2021-01-04 14:08

I am trying to apply the X Macro concept, in order to have the possibility to initialize all struct members to a custom default (invalid) value. I write the following code:<

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-04 14:50

    You might like the undef-free version of X_Macros,
    it reduces the necessary care taken with defining and undefining around each use
    and is better suited for definition in a header und useage in multiple code files:

    #define LIST_OF_STRUCT_MEMBERS_foo(mode) \  
        X_##mode(a) \  
        X_##mode(b) \  
        X_##mode(c)  
    
    #define X_struct(name) int name;  
    #define X_list(name) -1,  
    #define foo_DEFAULT_VALUE  { LIST_OF_STRUCT_MEMBERS_foo(list) }  
    
    struct foo {  
         LIST_OF_STRUCT_MEMBERS_foo(struct)  
    };  
    
    static inline void foo_invalidate(struct foo* in) {  
         *in = (struct foo){  
         LIST_OF_STRUCT_MEMBERS_foo(list)  
         };  
    }  
    
    static struct foo test = foo_DEFAULT_VALUE;  
    

    Output (gcc -E):

    struct foo {
         int a; int b; int c;
    };
    
    static inline void foo_invalidate(struct foo* in) {
         *in = (struct foo){
         -1, -1, -1,
         };
    }
    
    static struct foo test = { -1, -1, -1, };
    

提交回复
热议问题