<story><title>My favourite C++ footgun</title><url>https://dustri.org/b/my-favourite-c-footgun.html</url></story><parent_chain><item><author>overgard</author><text>My (least) favorite footgun is &amp;quot;auto&amp;quot; when it comes to references.&lt;p&gt;If you want to get a pointer from something, you would write this:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    auto fooPtr = mywidget.getPtr();
    fooPtr-&amp;gt;doCoolStuff();
&lt;/code&gt;&lt;/pre&gt;
But we all know references are better than pointers right? So we should just write...&lt;p&gt;&lt;pre&gt;&lt;code&gt;   auto fooRef = myWidget.getRef();
   fooRef.doCoolStuff()
&lt;/code&gt;&lt;/pre&gt;
The problem is... this is valid and will probably not do what you want. It will make a copy. What you want is actually&lt;p&gt;&lt;pre&gt;&lt;code&gt;   auto&amp;amp; fooRef = myWidget.getRef();
&lt;/code&gt;&lt;/pre&gt;
I once spent an entire day debugging a bizarre crash because of that. I was asking for a reference to a scene graph, and what was actually happening is I was getting a clone of the scene graph (which was an object that couldn&amp;#x27;t be safely copied), and then destroying a bunch of shared pointers when the function returned. Fun times.</text></item></parent_chain><comment><author>jchw</author><text>As others have pointed out, the idiomatic solution here is to delete the copy constructor if an instance is unsafe to copy — however, I suspect the &lt;i&gt;reason&lt;/i&gt; why auto behaves differently from other inference in requiring this to be explicit is probably something along the lines of making it harder to have a dangling reference. It’s shockingly easy to wind up with a dangling reference with fairly innocuous code, something like QString().toUtf8().data() so maybe this makes sense. (Doesn’t help for that case since it’s a pointer to raw data, but you get the picture.)</text></comment>
<story><title>My favourite C++ footgun</title><url>https://dustri.org/b/my-favourite-c-footgun.html</url></story><parent_chain><item><author>overgard</author><text>My (least) favorite footgun is &amp;quot;auto&amp;quot; when it comes to references.&lt;p&gt;If you want to get a pointer from something, you would write this:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    auto fooPtr = mywidget.getPtr();
    fooPtr-&amp;gt;doCoolStuff();
&lt;/code&gt;&lt;/pre&gt;
But we all know references are better than pointers right? So we should just write...&lt;p&gt;&lt;pre&gt;&lt;code&gt;   auto fooRef = myWidget.getRef();
   fooRef.doCoolStuff()
&lt;/code&gt;&lt;/pre&gt;
The problem is... this is valid and will probably not do what you want. It will make a copy. What you want is actually&lt;p&gt;&lt;pre&gt;&lt;code&gt;   auto&amp;amp; fooRef = myWidget.getRef();
&lt;/code&gt;&lt;/pre&gt;
I once spent an entire day debugging a bizarre crash because of that. I was asking for a reference to a scene graph, and what was actually happening is I was getting a clone of the scene graph (which was an object that couldn&amp;#x27;t be safely copied), and then destroying a bunch of shared pointers when the function returned. Fun times.</text></item></parent_chain><comment><author>criddell</author><text>If you develop on Windows and are using Visual Studio 2019
&amp;quot;auto fooRef = myWidget.getRef()&amp;quot; will get a little squiggle under it to warn you that you are making a copy.</text></comment>