title: C++ class override for const and non-const reference member
body: <p>So I'm making a bitmask class that stores a reference to an std::byte as a member and the index of the individual bit to allow accessing the value of that bit and also assigning to that bit. I also want it to be possible for the value of the std::byte passed to optionally be a const, and if it is a const, I want the class itself to be considered a const or at least make sure operations that may change the underlying value of the std::byte (such as assignment) do not work. However I don't see a way to implement it without copypasting code which I consider to be too complicated. Is there an easier way to get around this? This is my current code for the bitmask class:</p>
  <pre class="lang-cpp prettyprint-override"><code>class bitmask
  {
      public:
          bitmask(std::byte &amp;chunk, std::uint_fast8_t index) noexcept
              : _chunk(chunk), _index(index){};
          bitmask(bitmask const &amp;source) = default;
          
          operator bool() const noexcept
          {
              return static_cast&lt;bool&gt;((_chunk &gt;&gt; (7 - _index)) &amp; std::byte{1});
          }
          bitmask &amp;operator=(bool val) noexcept
          {
              _chunk = ((_chunk &amp; ~(std::byte{1} &lt;&lt; (7 - _index))) |
                        (std::byte{val} &lt;&lt; (7 - _index)));
              return *this;
          }
  
      private:
          std::byte &amp;_chunk;
          std::uint_fast8_t const _index;
  };
  </code></pre>
  <p>What I want is to basically make a variant of it where chunk is a const reference and the assignment operator doesn't exist, without copy-pasting existing code to avoid reptition.</p>
  <p>PS: I don't mind using any C++ standard, including C++20, as long as it solves the problem elegantly.</p>