#include <vector> struct A { int a[100]; }; void foo (const A& a) { std::vector<A> vA; vA.push_back(std::move(a)); // how does move really happen? } int main () { A a; foo(a); }
The above code compiles in order. Now everywhere it is written that moveavoids copying.Following are my inquiries:
move
const
eg.
void foo (A&& a) { // suppose we invoke this version std::vector<A> vA; vA.push_back(std::move(a)); // how copy is avoided? }
std::move . lvalue rvalue. const A && (, , ).
std::move
const A &&
std::vector a const A & a A &&, const A &, const A && const A &
std::vector
const A &
A &&
, const, / , - . ( , , )
A. A POD, , / A.
, , , reset 0. , , .
, . , .
#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)); } int main () { A a; foo(a); // "copy" bar(std::move(a)); // "move" }
Source: https://habr.com/ru/post/1651063/More articles:How to decode JWT (header and body) in java using Apache Commons codec? - apacheПуть к файлу Завершение в возвышенном тексте 3 - sublimetextConfigure xeditable locally - javascriptthis.setState () is not a function when using the reaction with jquery inside componentDidmount - jquerygit -flow: workflow for creating “release candidates” / QA web artifacts - gitI have a list of STL vectors, and I want to sort them by the first element of each vector - c ++Неразрывное пространство в конце строки - javascriptffmpeg concatenation after using drawtext filter - ffmpegGPathResult for String without XML declaration - xmlAkka.NET Remote between Linux and Windows - c #All Articles