Counting preprocessor macros

后端 未结 5 2008
离开以前
离开以前 2021-01-02 03:46

I have this macro code, which allows me to define both a C enum and a list of the enumerated names as strings using one construct. It prevents me from havi

5条回答
  •  失恋的感觉
    2021-01-02 04:18

    I know this isn't a complete answer. You can create a macro around something like this.

    #include 
    
    const char * array[] = {
      "arr1", "arr2", "arr3", "arr4"
    };
    
    int main (int argc, char **argv)$
    {
      printf("%d\n", sizeof(array)/sizeof(const char *));
    }
    

    If you can modify your enum so it has continous elements you can do sth like this (from Boost)

    enum { A=0,B,C,D,E,F,N };
    const char arr[N]; // can contain a character for each enum value
    

提交回复
热议问题