lundi 3 février 2020

How does move semantics apply on the following snippet when no xvalue is present?

I stumbled upon the following article and do not understand the performance difference between C++98 and C++11 that is, as the author says, attributed to move semantics.

#include <vector>

using namespace std;

int main() {
    vector<vector<int> > V;

    for(int k = 0; k < 100000; ++k) {
        vector<int> x(1000);
        V.push_back(x);
    }

    return 0;
}

To the best of my knowledge, V.push_back(x) does not invoke any move semantics. I believe that the x is an lvalue and this snippet is invoking the same vector::push_back(const T&) in both C++98 and C++11.

The code compiles identically on either version: https://godbolt.org/z/q3Lzae

Is the author incorrect with his statement, or is the compiler smart enough to realize x is about to be destroyed?

If the author is incorrect, is there anything else present in C++11 that would have given this the performance boost "without changing a line of code"?

Aucun commentaire:

Enregistrer un commentaire