Skip to content

Move

Move

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from one object to another.

1
#include <utility>

1
2
std::vector<double> v1(30, 1.0);
std::vector<double> v2 = v1;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
std::cout << "v1: ";
for (double x: v1) {
    std::cout << x << " ";
}
std::cout << '\n';

std::cout << "v2: ";
for (double x: v2) {
    std::cout << x << " ";
}
std::cout << '\n';

1
2
std::vector<double> v1(30, 1.0);
std::vector<double> v2 = v1;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
std::cout << "v1: ";
// NOLINTNEXTLINE(bugprone-use-after-move): making a point
for (double x: v1) {
    std::cout << x << " "; // prints nothing
}
std::cout << '\n';

std::cout << "v3: ";
for (double x: v3) {
    std::cout << x << " ";
}
std::cout << '\n';

Share Snippets