[C++]STD Move & Forward Notes
keywords: VS2019, Code Analysis, Code Checking, std::move
Docs
std::move
https://en.cppreference.com/w/cpp/utility/move
std::forward
https://en.cppreference.com/w/cpp/utility/forward
Move assignment operator
https://en.cppreference.com/w/cpp/language/move_assignment
Move with vector::push_back
https://stackoverflow.com/a/11572684/1645289
Tools
New Code Analysis Checks in Visual Studio 2019: use-after-move and coroutine
https://blogs.msdn.microsoft.com/vcblog/2019/01/28/new-code-analysis-checks-in-visual-studio-2019-use-after-move-and-coroutine
Move constructors(since C++11)
https://en.cppreference.com/w/cpp/language/move_constructor
Examples
std::move with reference
#include <vector>
#include <iostream>
struct A {
int a[100];
A() {}
A(const A& other) {
std::cout << "copy" << std::endl;
}
A(A&& other) {
std::cout << "move" << std::endl;
}
};
void foo(const A& a) {
std::vector<A> vA;
vA.push_back(std::move(a));
}
void bar(A&& a) {
std::vector<A> vA;
vA.push_back(std::move(a));
}
void baz(A& a) {
std::vector<A> vA;
vA.push_back(std::move(a));
}
int main() {
A a;
foo(a); // "copy"
foo(std::move(a)); // "copy"
bar(std::move(a)); // "move"
//baz(std::move(a)); // compilation error!!
}
origin: Does std::move
work with lvalue references?
https://stackoverflow.com/a/49305927/1645289
当我们表达的时候,我们应当像文学一样说话,而不让文学像我们的生活一样。