title: `container_of` macro in C++ with the same signature as C's
body: <p>I have code that uses the famous <code>container_of</code> macro to implement a macro-only linked list library.</p>
  
  <p>It works perfectly in C. Now I want to support C++ on it, so I need a <code>container_of</code> replacement for C++ that matches the following signature:</p>
  
  <pre><code>container_of(ptr, type, member)
  </code></pre>
  
  <p>The C implementation is this:</p>
  
  <pre><code>#define container_of(ptr, type, member) ({ \
              const typeof( ((type *)0)-&gt;member ) *__mptr = (ptr); 
              (type *)( (char *)__mptr - offsetof(type,member) );})
  </code></pre>