title: Is it safe to use an enum in a bit field?
body: <p>Say, I've got the following struct:</p>
  
  <pre><code>typedef struct my_struct{
      unsigned long       a;
      unsigned long       b;
      char*               c;
      unsigned int        d1  :1;
      unsigned int        d2  :4;
      unsigned int        d3  :4;
      unsigned int        d4  :23;
  } my_type, *p_type;
  </code></pre>
  
  <p>The field <code>d3</code> is currently defined by <code>#define</code>s that reach from <code>0x00</code> until <code>0x0D</code>.</p>
  
  <p>Actually, <code>d3</code> is an enumeration. So it's tempting to go ahead and replace </p>
  
  <pre><code>    unsigned int        d3  :4;
  </code></pre>
  
  <p>by</p>
  
  <pre><code>    my_enum             d3  :4;
  </code></pre>
  
  <p>Is this safe/allowed?</p>
  
  <p>The code has to compile with various</p>
  
  <ul>
  <li>compilers (GCC, Visual Studio, embedded stuff)</li>
  <li>platforms (Win32, Linux, embedded stuff)</li>
  <li>configurations (compile as C, compile as C++) </li>
  </ul>
  
  <p>Obviously, I could leave the definition of <code>d3</code> as it is and use the enum in my code, assign it to <code>d3</code> and so on but that's not going to work with C++.</p>