title: How can I make my class immune to the "auto value = copy of proxy" landmine in C++?
body: <p>I have a fairly complex maths library I'm working on, and I have discovered a nasty bug when client code uses auto. Halfway through creating a minimal reproductive case to ask a question about it, I realise I can reproduce something similar using the standard library alone. See this simple test case:</p>
  <pre><code>#include &lt;vector&gt;
  #include &lt;assert.h&gt;
  
  int main()
  {
      std::vector&lt;bool&gt; allTheData = {true, false, true};
  
      auto boolValue = allTheData[1]; // This should be false - we just declared it.
      assert(boolValue == false);
      boolValue = !boolValue;
      assert(boolValue == true);
  
      assert(allTheData[1] == false); // Huh? But we never changed the source data! Only our local copy.
  }
  </code></pre>
  <p><a href="https://godbolt.org/z/hsKTrTxKd" rel="noreferrer">Live on Godbolt</a>. (Fun fact: Clang actually optimises this to writing &quot;7&quot; - 3 true bits - and a call to __assert_fail.)</p>
  <p>(<em>Yes I know std::vector&lt;bool&gt; sucks</em> - but in this case it's handy to create a minimum reproducible example that's only a few lines long) Here's a <a href="https://godbolt.org/z/YzP57vnbf" rel="noreferrer">longer example that doesn't use std::vector&lt;bool&gt;</a>, and uses a custom container type, with assignment and copy/move deleted, and still shows the problem.</p>
  <p>I understand what is going on under the hood, there's a proxy class returned by operator[] meant to implement <code>allTheData[1] = true</code> and related functionality, the client code that is written as if it's reading the value is actually storing the proxy in boolValue, and then when the client later modifies what it thinks is a bool, the original source data is modified instead. <strong>TLDR: 'auto' copied the proxy.</strong></p>
  <p>The code did what the programmer told it to do, not what the programmer meant.</p>
  <p>If the programmer wanted boolValue's changes to update the source data, they would've done <code>auto&amp; boolValue = ...</code>, which works with <code>operator[]</code> implementations returning <code>T&amp;</code>, but not those needing custom proxies which fake reference-like behavior.</p>
  <p>All the copy and move constructors and both assignment operators for the proxy are declared private (have also tried <code>= delete</code>), but this bug isn't caught at compile time. The proxy is copied regardless of whether the copy constructor is deleted.</p>
  <p>All the &quot;fixes&quot; I've found for this bug focus on the client part of the code. They're things like: &quot;don't use auto&quot;, &quot;cast to the underlying type&quot;, &quot;access through a const ref&quot;, etc. These are all substandard fixes, once you discover the bad behavior you can add one of these as a hack fix, but the underlying problem remains to catch the next unsuspecting user.</p>
  <p>I'd rather remove the landmine than keep bypassing it, and putting up a sign saying &quot;don't use auto&quot;, or &quot;always use const&quot;, just marks the minefield, it doesn't remove it.</p>
  <h3>How can I make my library immune to this gotcha? (Without changing the client code!)</h3>
  <ul>
  <li>First preference would be the code works as written - <code>assert(allTheData[1] == false)</code> passes
  <ul>
  <li>A way to define the decay type of the proxy when it's written to auto?. So <code>decltype(boolValue)</code> is <code>bool</code>?</li>
  <li>An implicit conversion operator that takes precedence over copying?</li>
  <li>Any other way to make this pass without changing the code snippet above?</li>
  </ul>
  </li>
  <li>Second preference is there a way to make writing a proxy to a variable a compile error?
  <ul>
  <li>I'm declaring copy and move constructors as delete, and move and copy assignment operators as delete. Still compiles.</li>
  <li>Is there anyway to declare a class as unable to become a lvalue?</li>
  </ul>
  </li>
  <li>Is there anything in proposed c++ future standards that will fix this?</li>
  </ul>
  <hr />
  <p>Also an issue is code like:</p>
  <pre><code>std::vector&lt;bool&gt; ReadFlags();
  ... later ...
  auto databaseIsLockedFlag = ReadFlags()[FLAG_DB_LOCKED];
  if (databaseIsLockedFlag) &lt;-- Crash here. Proxy has outlived temporary vector.
  </code></pre>
  <p>I'm only using vector here as it's a really simple example of the problem. This isn't a bug with vector, this is a bug with the proxy type pattern, of which vector is an example to show the problem.</p>
  <p>Strangely enough MSVC's Intellisense engine <strong>sometimes</strong> reports copying a no-move-no-copy proxy type as a compile error, but <strong>then compiles it fine anyway</strong>:</p>
  <p><a href="https://i.stack.imgur.com/0OzTi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/0OzTi.png" alt="enter image description here" /></a><br />
  <em>It'd be really nice if this intellisense compile error was a real compile error. Sigh</em></p>