title: Passing reference of packed struct member to template. gcc bug?
body: <p>I encountered a problem, passing struct member to a template function. The function's goal is to take the address and size of the member. Here is simple example:</p>
  
  <p>This is the struct. It has packed attribute.</p>
  
  <pre><code>struct TestStruct {
      unsigned char       elem1;
      unsigned char       elem2;
      uint64_t            elem3;
      char                buf[10000];
      int                 elem4;
      unsigned char       elem5;
  }
  __attribute__ ((packed));
  </code></pre>
  
  <p>this is the template function, which should get a member's address</p>
  
  <pre><code>template&lt;typename T&gt;
  void addData(const T &amp;val)
  {
          printf ("address inside func: %p \n",&amp;val);
  }
  
  int main(int argc, char *argv[])
  {
          TestStruct testdata;
          testdata.elem4 = 0;
          printf ("struct address is:   %p \n",&amp;testdata);
          printf ("elem4  address is:   %p \n",&amp;testdata.elem4);
          addData(testdata.elem4);
          return 0;
  }
  </code></pre>
  
  <p>The problem: When  <strong>attribute</strong> ((packed)); is set (like in the example)
  the template function receives wrong address of the member:</p>
  
  <p>Output:</p>
  
  <pre><code>struct address is:   0x7fff735bb4e0 
  elem4  address is:   0x7fff735bdbfa 
  address inside func: 0x7fff735bb4dc
  </code></pre>
  
  <p>If I remove the "packed" attribute, everything is OK.
  There is no error and no warning (even with -Wall -Wextra), but not the right address is passed to the function.</p>
  
  <p>I read this:</p>
  
  <p><a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36566" rel="noreferrer">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36566</a></p>
  
  <p>and found that there is an issue, getting references to packed-struct members.
  Interesting enough, replacing const T&amp; with T&amp; in my template function, produces the error message:</p>
  
  <pre><code>error: cannot bind packed field ‘testdata.TestStruct::elem4’ to ‘int&amp;’
  </code></pre>
  
  <p>So, I have 2 questions:</p>
  
  <ol>
  <li><p>Why cannot packed-struct members be passed as const references, when their address can be passed as pointer</p></li>
  <li><p>What happens in the const T&amp; case? There is no error, no warning, but the incorrect address is passed to the function. As we know, the address of reference is the address of the variable, the reference points to.</p></li>
  </ol>