Somewhat surprisingly (to me), the following two programs compile to different outputs, with the latter one having much better performance (tested with gcc and clang):
#include <vector>
int main()
{
std::vector<int> a(2<<20);
for(std::size_t i = 0; i != 1000; ++i) {
std::vector<int> b(2<<20);
a = b;
}
}
vs.
#include <vector>
int main()
{
std::vector<int> a(2<<20);
for(std::size_t i = 0; i != 1000; ++i) {
std::vector<int> b(2<<20);
a = std::move(b);
}
}
Could someone explain to me why the compiler does (or can) not automatically consider b
an xvalue in the last assignment and apply move semantics without the explicit std::move
cast?
Edit: Compiled with (g++|clang++) -std=c++11 -O3 -o test test.cpp
std::move(b)
is an xvalue,b
is not – Dietrich