5

In C++11 you can use std::vector::push_back in combination with std::move to avoid copies while inserting elements into a vector. Is there a section in the standard that forbids compilers to use std::move automatically with local variables that are not used after calls to push_back? When filling vectors with large elements, this could be a huge performance benefit.

I checked the following code with gcc 7.1.0 and -O3 and with Version 1 it prints move while Version 2 prints copy.

#include <iostream>
#include <string>
#include <vector>

struct S
{
    S() = default;
    S(S const&) { std::cout << "copy" << std::endl; }
    S(S &&) { std::cout << "move" << std::endl; }
    std::string a;
    std::string b;
};

void fill_members(S& s) { /*...*/ }

int main()
{
    std::vector<S> v;
    {
        S s;

        // Somehow fill members of s, maybe use a function call for that.
        fill_members(s);

        // Version 1:
        // This avoids a copy, since there is an explicit std::move.
        v.push_back(std::move(s));

        // Version 2:
        // Why dont compilers optimize this with std::move?
        // The compiler can see that s is not used after this line.
        v.push_back(s);
    }
}
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
pschill
  • 5,055
  • 1
  • 21
  • 42
  • [Can compiler generate std::move for a last use of lvalue automatically?](https://stackoverflow.com/questions/15387271/can-compiler-generate-stdmove-for-a-last-use-of-lvalue-automatically) [Why can't the last assignment from a variable in a function be treated as a move?](https://stackoverflow.com/questions/30741734/why-cant-the-last-assignment-from-a-variable-in-a-function-be-treated-as-a-move) – cpplearner Jul 28 '17 at 08:06
  • I've withdrawn my answer as the answer on the duplicate is far better. @cpplearner: thanks for spotting it. – Bathsheba Jul 28 '17 at 08:11

0 Answers0