vendredi 28 juillet 2017

Why is vector::push_back of local variable not move optimized?

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);
    }
}

Aucun commentaire:

Enregistrer un commentaire