Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::pair move not elided on definition?
    text
    copied!<p>I noticed something very strange with Visual Studio 2012: Defining a pair object like so:</p> <pre><code> auto objp = pair&lt;int, LogMe&gt;(); </code></pre> <p>will <em>not</em> elide the copy/move of the pair in VC11, this call will print:</p> <pre><code>LogMe::LogMe - def.ctor! LogMe::LogMe - move.ctor! LogMe::~LogMe - dtor! </code></pre> <p>that is, a temporary pair <em>will</em> be created and then moved into the objp variable. (declaring it as <code>pair&lt;...&gt; obj;</code> only logs the default ctor)</p> <p>I have cross checked with my LogMe test object alone:</p> <pre><code>cout &lt;&lt; "# Construct Object via auto obj = ...\n"; auto obj = LogMe(); # Construct Object via auto obj = ... LogMe::LogMe - def.ctor! </code></pre> <p>and here the assignment will be elided.</p> <p>This seems to be specific to VC11, as <a href="http://ideone.com/wXuU6o" rel="noreferrer">testing it in IDEOne</a> (which uses gcc 4.8.1) shows that the extraneous move is elided always there.</p> <p><strong>What's going on here?</strong> <em>Not being able to rely on the initialization copy being elided makes me nervous.</em></p> <p>Note: Tests for release vs. debug version show the same result. (Which I would have expected, as copy-elision is performed independently of optimization flags in MSVC.)</p> <hr> <p>Full sourcecode to test (See also the <a href="http://ideone.com/wXuU6o" rel="noreferrer">ideone link</a>):</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;map&gt; using namespace std; struct LogMe { std::string member; LogMe() { cout &lt;&lt; __FUNCTION__ &lt;&lt; " - def.ctor!" &lt;&lt; endl; } ~LogMe() { cout &lt;&lt; __FUNCTION__ &lt;&lt; " - dtor!" &lt;&lt; endl; } LogMe(LogMe const&amp;) { cout &lt;&lt; __FUNCTION__ &lt;&lt; " - cpy.ctor!" &lt;&lt; endl; } LogMe&amp; operator=(LogMe const&amp;) { cout &lt;&lt; __FUNCTION__ &lt;&lt; " - cpy.assign.op!" &lt;&lt; endl; return *this; } LogMe(LogMe&amp;&amp;) { cout &lt;&lt; __FUNCTION__ &lt;&lt; " - move.ctor!" &lt;&lt; endl; } LogMe&amp; operator=(LogMe&amp;&amp;) { cout &lt;&lt; __FUNCTION__ &lt;&lt; " - move.assign.op!" &lt;&lt; endl; return *this; } }; int _tmain(int argc, _TCHAR* argv[]) { { cout &lt;&lt; "# Construct Object via auto obj = ...\n"; auto obj = LogMe(); cout &lt;&lt; "# Construct pair&lt;int, object&gt; via auto objp = ...\n"; auto objp = pair&lt;int, LogMe&gt;(); cout &lt;&lt; "# Construct pair&lt;int, object&gt; via pair objp2; ...\n"; pair&lt;int, LogMe&gt; p2; } return 0; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload