title: What is wrong with this use of offsetof?
body: <p>I'm compiling some c++ code in MinGW GCC 4.4.0, and getting warnings with the following form...</p>
  
  <pre><code>warning: invalid access to non-static data member '&lt;membername&gt;'  of NULL object
  warning: (perhaps the 'offsetof' macro was used incorrectly)
  </code></pre>
  
  <p>This problem seems familiar - something I've tried to resolve before and failed, I think, but a while ago. The code builds fine in Visual C++, but I haven't built this particular code recently in any other compiler.</p>
  
  <p>The problem code is the following template...</p>
  
  <pre><code>template&lt;typename T&gt;
  class c_Align_Of
  {
    private:
      struct c_Test
      {
        char m_Char;
        T    m_Test;
      };
    public:
      enum { e_Align = offsetof (c_Test, m_Test) };
  };
  </code></pre>
  
  <p>Obviously I can probably use some conditional compilation to use compiler-specific functions for this, and I believe C++0x will (at long last) make it redundant. But in any case, I cannot see anything wrong with this use of <code>offsetof</code>.</p>
  
  <p><em>Very</em> pedantically, it's possible that because the <code>T</code> parameter types are sometimes non-POD, so GCC classes <code>c_Test</code> as non-POD and complains (and complains and complains - I'm getting nearly 800 lines of these warnings).</p>
  
  <p>This is naughty by the strict wording of the standard, since non-POD types can break <code>offsetof</code>. However, this kind of non-POD shouldn't be a problem in practice - <code>c_Test</code> will not have a virtual table, and no run-time trickery is needed to find the offset of <code>m_Test</code>.</p>
  
  <p>Besides, even if <code>c_Test</code> had a virtual table, GCC implements the offsetof macro using an intrinsic that is always evaluated at compile-time based on the static layout of that particular type. Providing a tool then whining (sorry, warning) every time it's used just seems silly.</p>
  
  <p>Also, I'm not the only person around here who does this kind of thing...</p>
  
  <p><a href="https://stackoverflow.com/questions/1453393/legit-uses-of-the-offsetof-macro-in-c-c/1455192#1455192">Answer to legit-uses-of-offsetof question</a></p>
  
  <p>I <em>do</em> remember having an issue with <code>offsetof</code> for this kind of reason, but I don't <em>think</em> the problem was this template.</p>
  
  <p>Any ideas?</p>