News for hackers. Stuff that matters.
Re:Type-safe generic data structures in C by Galanwe on Friday April 09, 2021 @12:28AM (#26746722)
I share your feelings, though some years ago I discovered the neat trick of "xmacros".

In a nutshell:

    #define COLOR_LIST \
      X( RED    )      \
      X( GREEN  )      \
      X( BLUE   )      \
      X( PURPLE )

    enum Color
    {
    #define X(code) code,
      COLOR_LIST
    #undef X
    };

    static const char* colorNames[] =
    {
    #define X(code) #code,
      COLOR_LIST
    #undef X
    };
You can even make it cleaner and store more metadata:

    // --- color.def
    X( RED,    "Red",   0xFF0000 )
    X( GREEN,  "Green", 0x00FF00 )
    X( BLUE,   "Blue",  0x0000FF )

    // --- color.h
    enum Color
    {
    #define X(code,name,mask) code,
    #  include "color.def"
    #endif
    };

    static const char* colorNames[] =
    {
    #define X(code,name,mask) name,
    #  include "color.def"
    #undef X
    };

    static unsigned int colorMasks[] =
    {
    #define X(code,name,mask) mask,
    #  include "color.def"
    #undef X
    };
Re:Type-safe generic data structures in C by tempodox on Friday April 09, 2021 @03:26AM (#26747608)
Yep, good old trick. Just don't forget to

  #undef X
when you're done.