title: Resolving a compiler error due to an invariant member with a possible deleted default constructor
body: <p>I have asked a series of questions that all relate to the same source code in this order:</p>
  
  <blockquote>
    <ol>
    <li><a href="https://stackoverflow.com/questions/55988878/experimenting-with-unions-and-bitfields-within-a-structures-and-templates">experimenting-with-unions-and-bitfields-within-a-structures-and-templates</a></li>
    <li><a href="https://stackoverflow.com/questions/55998550/trying-to-flip-the-order-of-bits-in-stdbitset">trying-to-flip-the-order-of-bits-in-stdbitset</a></li>
    <li><a href="https://stackoverflow.com/questions/55999515/avoiding-ambiguity-in-overload-resolution">avoiding-ambiguity-in-overload-resolution</a></li>
    </ol>
  </blockquote>
  
  <p>I've also asked these series of questions over at Code Review that are also related.</p>
  
  <blockquote>
    <ol>
    <li><a href="https://codereview.stackexchange.com/questions/219736/emulating-virtual-registers-by-experimenting-with-unions-bitfields-structs-and">emulating-virtual-registers-by-experimenting-with-unions-bitfields-structs-and-template-specialization</a></li>
    <li><a href="https://codereview.stackexchange.com/questions/219774/emulating-virtual-registers-part-2">emulating-virtual-registers-part-2</a></li>
    </ol>
  </blockquote>
  
  <p>This should give you an overview of my original code design and it is there for reference and background information. Since then I started to look at my working code and wanted to simplify it even more.</p>
  
  <p>I then decided to remove the template specializations and have my Register class be 64 bits wide instead of 8 bits by default while specializing higher order sized registers. </p>
  
  <p>I'm trying to incorporate the idea that one can access the data in any of these manners:</p>
  
  <ul>
  <li>Full value:  - the entire   64 bit qword</li>
  <li>Half value(s):   - 2 individual 32 bit dwords </li>
  <li>Quarter value(s) - 4 individual 16 bit words </li>
  <li>Eighth value(s)  - 8 individual  8 bit bytes </li>
  </ul>
  
  <p>And through the utility use of <code>std::bitset</code> one can easily access any of the bits within the full 64 bit register. With the use of unions I should be able to correctly map the memory so that a register can be represented as and accessed by any of these combinations: </p>
  
  <ul>
  <li><code>std::bitset&lt;64&gt; qword</code></li>
  <li><code>std::bitset&lt;32&gt; dword[2]</code></li>
  <li><code>std::bitset&lt;16&gt; word[4]</code></li>
  <li><code>std::bitset&lt;8&gt;  byte[8]</code></li>
  </ul>
  
  <p>The concept of using the union is to have a single space in memory that represents 64 bits for any given register. Now I'm trying to keep my Register class trivially copyable.</p>
  
  <p>So I had modified the original code from what can be found throughout the links above to a simpler version of this:</p>
  
  <p><strong>Register.h</strong></p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;bitset&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  namespace vpc {
      typedef std::int8_t  i8;
      typedef std::int16_t i16;
      typedef std::int32_t i32;
      typedef std::int64_t i64;
  
      const std::uint16_t BYTE = 0x08;
      const std::uint16_t WORD = 0x10;
      const std::uint16_t DWORD = 0x20;
      const std::uint16_t QWORD = 0x40;
  
      typedef std::bitset&lt;BYTE&gt;  Byte;
      typedef std::bitset&lt;WORD&gt;  Word;
      typedef std::bitset&lt;DWORD&gt; DWord;
      typedef std::bitset&lt;QWORD&gt; QWord;
  
      union Bits {
          QWord value;
          DWord dword[2];
          Word  word[4];
          Byte byte[8];
      };
  
      struct Register {
          Bits bits;
          Register() = default;
      };       
  
  } // namespace vpc
  </code></pre>
  
  <p>Then I want to test to make sure that everything so far is trivially copyable. So I run this short program.</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  #include "Register.h"
  
  int main() {
      using namespace vpc;
  
      std::cout &lt;&lt; std::boolalpha;
  
      std::cout &lt;&lt; "std::bitset&lt;64&gt; is trivially copyable " 
          &lt;&lt; std::is_trivially_copyable&lt;std::bitset&lt;64&gt;&gt;::value &lt;&lt; '\n'
                &lt;&lt; "QWord is trivially copyable "
          &lt;&lt; std::is_trivially_copyable&lt;QWord&gt;::value &lt;&lt; '\n'
                &lt;&lt; "DWord is trivially copyable "
          &lt;&lt; std::is_trivially_copyable&lt;DWord&gt;::value &lt;&lt; '\n'
                &lt;&lt; "Word is trivially copyable "
          &lt;&lt; std::is_trivially_copyable&lt;Word&gt;::value &lt;&lt; '\n'
                &lt;&lt; "Byte is trivially copyable "
          &lt;&lt; std::is_trivially_copyable&lt;Byte&gt;::value &lt;&lt; '\n'
                &lt;&lt; "Bits is trivially copyable "
          &lt;&lt; std::is_trivially_copyable&lt;Bits&gt;::value &lt;&lt; '\n'
                &lt;&lt; "Register is trivially copyable "
          &lt;&lt; std::is_trivially_copyable&lt;Register&gt;::value &lt;&lt; '\n';
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>And I get this output:</p>
  
  <pre><code>std::bitset&lt;64&gt; is trivially copyable true
  QWord is trivially copyable true
  DWord is trivially copyable true
  Word is trivially copyable true
  Byte is trivially copyable true
  Bits is trivially copyable true
  My Register is trivially copyable true
  </code></pre>
  
  <p>Now when looking at the union Bits it states that it is trivially copyable. So instead of declaring a variable of type Bits in the struct as its data member; I believe we should be able to have an anonymous union within our structure so we can get right to our qword, dwords, words and bytes directly. So now the class will look like this:</p>
  
  <pre><code>struct Register {
      union {
          QWord value;
          DWord dword[2];
          Word  word[4];
          Byte  byte[8];
      };
  
      Register() = default;
  };
  </code></pre>
  
  <p>Then I run this line of code in our main.cpp</p>
  
  <pre><code>// code...
  
  std::cout &lt;&lt; std::boolalpha;
  std::cout &lt;&lt; "Register is trivially copyable "
            &lt;&lt; std::is_trivially_copyable&lt;Register&gt;::value &lt;&lt; '\n';
  
  // code...
  </code></pre>
  
  <p>And I get this output:</p>
  
  <pre><code>Register is trivially copyable true
  </code></pre>
  
  <p>Okay so far so good. </p>
  
  <p>Now I'm working on my function that operates on a Register object. It will reverse the order of the bits as seen from the previously asked questions. Except in this case I'm not using templates. So here I declare the function prototype in <code>Register.h</code> after my class:</p>
  
  <pre><code>Register reverseBitOrder( Register&amp; reg, bool copy = false );
  </code></pre>
  
  <p>And then I created a <code>Register.cpp</code> file just to implement this function.</p>
  
  <p><strong>Register.cpp</strong></p>
  
  <pre><code>#include "Register.h"
  
  namespace vpc {
  
      Register reverseBitOrder(Register&amp; reg, bool copy) {
          auto str = reg.value.to_string();
          std::reverse(str.begin(), str.end());
  
          if (copy) { // return a copy
              Register cpy;
              cpy.value = QWord(str);
              return cpy;
          } else {
              reg.bits.value = QWord(str);
              return { 0 };
          }
      }
  
  } // namespace vpc
  </code></pre>
  
  <p>So now that I have my function written I clean my solution and now I try to compile "Register.h". However; I'm getting this compiler error frpm Visual Studio 2017 with the language setting set to latest draft standard or flag <code>(/std:c++latest)</code>.</p>
  
  <pre><code>--- Build started: Project: Corgi64, Configuration: Debug Win32 ------
  1&gt;Register.cpp
  1&gt;c:\***\register.cpp(10): error C2280: 'vpc::Register::Register(void)': attempting to reference a deleted function
  1&gt;c:\***\register.h(40): note: see declaration of 'vpc::Register::Register'
  1&gt;c:\***\register.h(40): note: 'vpc::Register::Register(void)': function was implicitly deleted because 'vpc::Register' has a variant data member 'vpc::Register::value' with a non-trivial default constructor
  1&gt;c:\***\register.h(34): note: see declaration of 'vpc::Register::value'
  1&gt;Done building project "Corgi64.vcxproj" -- FAILED.
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>So when I click on error C2280 it directs me to the declaration of my Register variable <code>cpy</code>. When I move my mouse cursor overtop of the variable <code>cpy</code> it gives me the message:</p>
  
  <blockquote>
    <p>vpc::Register cpy <p>
     the default constructor of "vpc::Register" cannot be referenced -- it is a deleted function</p>
  </blockquote>
  
  <p>So my question becomes: Why is the default constructor being deleted if everything from before was trivially copyable? Since I am now using it within a function, all of a sudden, it is stating that my structure has an invariant member that doesn't have a trivially copyable constructor and it is pointing to <code>Register::value</code> as the culprit. What is causing this, how and why? What can I do to fix or resolve this? </p>