title: Is this usage of a union in C++ correct?
body: <p>I am working on some C++ code for small microcontrollers (AVR). We use a 
  <code>struct MCU</code>to represent the register layout of the MCU. In the past all registers were of type <code>uint8_t</code> and we use the manufacture provided <code>cpp</code>-macros to set inidividual bits. This not the C++ way ...</p>
  
  <p>We introduced the <code>ControlRegister</code> class template: it can be parametrized with the acceptable value type: </p>
  
  <pre><code>template&lt;typename Component, typename BitType, typename ValueType = uint8_t&gt;
  struct __attribute__((packed)) ControlRegister final {
      typedef Component component_type;
      typedef ValueType value_type;    
      typedef BitType bit_type;
  
      template&lt;typename... Flags&gt;
      void inline set(Flags... f) {
          static_assert((std::is_same&lt;bit_type, Flags&gt;::value &amp;&amp; ... &amp;&amp; true), "wrong flag type");
          static_assert(sizeof...(Flags) &lt;= 8, "too much flags");
          value_type v = (static_cast&lt;value_type&gt;(f) | ... | 0);
          assert(Util::numberOfOnes(v) == sizeof... (Flags));
          hwRegister = v;
      }
      template&lt;BitType... Flags&gt;
      void inline add() {
          static_assert(sizeof...(Flags) &lt;= 8, "too much flags");
          constexpr auto v = (static_cast&lt;value_type&gt;(Flags) | ... | 0);
          constexpr auto n = Util::numberOfOnes(v);        
          static_assert(n == sizeof... (Flags), "use different flags");
          hwRegister |= v;
      }
  private:    
      volatile value_type hwRegister;
  };
  </code></pre>
  
  <p>Some MCU registers have a two-fold usage, e.g. setting a timer prescaler and some other interrupt information. The prescaler bits are used at several places and we thought it would be better to divide the register into two or more parts resembling the structure of the particular register. Since this is essentially the very same memory location the idea to use a union came into mind:</p>
  
  <pre><code>struct MCU final {
      struct CompA {
          enum class Flags1 {
              f1 = (1 &lt;&lt; 0),
              f2 = (1 &lt;&lt; 1)
          };
          enum class Flags2 {
              f1 = (1 &lt;&lt; 1),
              f2 = (1 &lt;&lt; 2)
          };
          union {
              ControlRegister&lt;CompA, Flags1&gt; part1;
              ControlRegister&lt;CompA, Flags2&gt; part2;
          } __attribute__ ((packed));
      };    
  };
  </code></pre>
  
  <p>The <code>part2</code>-member can be the forementioned prescaler component: the same value-type <code>Flags</code> can be used in several places.</p>
  
  <p>I've read several posts about unions in C++, but I still have no clear inbterpretation if this code fragment yields to UB. </p>
  
  <p>Looking at the assembler-language output of g++ leads to the assumption, that it is ok. But I'm not sure ...! </p>
  
  <pre><code>int main() {
      const auto c = reinterpret_cast&lt;MCU::CompA*&gt;(0x28);
  
      using F1 = MCU::CompA::Flags1;
      c-&gt;part1.set(F1::f1, F1::f1); // writing
  
      using F2 = MCU::CompA::Flags2;
      c-&gt;part2.set(F2::f1); 
  
      c-&gt;part2.add&lt;F2::f2&gt;(); // reading
  
      while(true) {}
  }
  </code></pre>