title: Type-safe generic containers with macros
body: <p>I'm trying to make a type-safe generic linked list in C using macros. It should work similarly  to how templates work in C++. For example,</p>
  
  <pre><code>LIST(int) *list = LIST_CREATE(int);
  </code></pre>
  
  <p>My first attempt was for <code>#define LIST(TYPE)</code> (the macro I used above) to define a <code>struct _List_##TYPE {...}</code>. That, however, did not work because the struct would be redefined every time I declared a new list. I remedied the problem by doing this:</p>
  
  <pre><code>/* You would first have to use this macro, which will define
     the `struct _List_##TYPE`...                               */
  DEFINE_LIST(int);
  
  int main(void)
  {
      /* ... And this macro would just be an alias for the struct, it
         wouldn't actually define it.                                  */
      LIST(int) *list = LIST_CREATE(int);
      return 0;
  }
  
  /* This is how the macros look like */
  
  #define DEFINE_LIST(TYPE)    \
      struct _List_##TYPE      \
      {                        \
          ...                  \
      }
  
  #define LIST(TYPE)       \
      struct _List_##TYPE
  </code></pre>
  
  <p>But another problem is that when I have multiple files that use <code>DEFINE_LIST(int)</code>, for example, and some of them include each other, then there will still be multiple definitions of the same struct. Is there any way to make <code>DEFINE_LIST</code> check if the struct has already been defined?</p>
  
  <pre><code>/* one.h */
  DEFINE_LIST(int);
  
  /* two.h */
  #include "one.h"
  DEFINE_LIST(int); /* Error: already defined in one.h */ 
  </code></pre>