Why is the vector :: push_back of a local variable not optimized?

In C ++ 11, you can use std::vector::push_back in combination with std::move to avoid copying when inserting elements into a vector. Is there a section in the standard that prohibits compilers from using std::move automatically with local variables that are not used after calls to push_back ? When filling vectors with large elements, this can be a huge advantage.

I checked the following code with gcc 7.1.0 and -O3 , and with version 1 it prints move , and 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); } } 
+5
source share

Source: https://habr.com/ru/post/1270310/


All Articles