Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this correct usage of C++ 'move' semantics?
    primarykey
    data
    text
    <p>Tonight I've been taking a look at some code I've been working on over the last few days, and began reading up on move semantics, specifically std::move. I have a few questions to ask you pros to ensure that I am going down the right path and not making any stupid assumptions!</p> <p>Firstly:</p> <p><strong>1) Originally, my code had a function that returned a large vector:</strong></p> <pre><code>template&lt;class T&gt; class MyObject { public: std::vector&lt;T&gt; doSomething() const; { std::vector&lt;T&gt; theVector; // produce/work with a vector right here return(theVector); }; // eo doSomething }; // eo class MyObject </code></pre> <p>Given "theVector" is temporary in this and "throw-away", I modified the function to:</p> <pre><code> std::vector&lt;T&gt;&amp;&amp; doSomething() const; { std::vector&lt;T&gt; theVector; // produce/work with a vector right here return(static_cast&lt;std::vector&lt;T&gt;&amp;&amp;&gt;(theVector)); }; // eo doSomething </code></pre> <p>Is this correct? Any pitfalls in doing it this way?</p> <p><strong>2) I noticed in a function I have that returns <code>std::string</code> that it automatically called the move constructor. Debugging in to Return of the String (thankyou, Aragorn), I noticed it called an explicit move constructor. Why is there one for the string class and not vector?</strong></p> <p>I didn't have to make any modifications to this function to take advantage of move semantics:</p> <pre><code>// below, no need for std::string&amp;&amp; return value? std::string AnyConverter::toString(const boost::any&amp; _val) const { string ret; // convert here return(ret); // No need for static_cast&lt;std::string&amp;&amp;&gt; ? }; // eo toString </code></pre> <p><strong>3) Finally, I wanted to do some performance tests, is the amazingly-fast results I got because of std::move semantics or did my compiler (VS2010) do some optimizing too?</strong></p> <p>(Implementation of <code>_getMilliseconds()</code> omitted for brevity)</p> <pre><code>std::vector&lt;int&gt; v; for(int a(0); a &lt; 1000000; ++a) v.push_back(a); std::vector&lt;int&gt; x; for(int a(0); a &lt; 1000000; ++a) x.push_back(a); int s1 = _getMilliseconds(); std::vector&lt;int&gt; v2 = v; int s2 = _getMilliseconds(); std::vector&lt;int&gt; v3 = std::move(x); int s3 = _getMilliseconds(); int result1 = s2 - s1; int result2 = s3 - s2; </code></pre> <p>The results were, obviously, awesome. result1, a standard assignment, took 630ms. The second result, was 0ms. Is this a good performance test of these things?</p> <p>I know some of this is obvious to a lot of you, but I want to make sure I understand the semantics right before I go blazer on my code.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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