🐭 移动操作的本质
移动语义可以说是现代 C++ 中最重要的特性之一,它解决了 C++ 中一直被人诟病的临时对象拷贝问题,让很多昂贵的拷贝操作变成了简单的移动操作,大大提升了 C++ 的性能。对老于代码,哪怕只是重新使用现代 C++ 编译器编译一遍都会有性能上的提升。
移动操作的本质:
C++ 中的大对象,比如说容器,在逻辑上一般由两个组成部分:比较小的「索引」部分与比较大的数据部分。数据部分一般存放在堆上,而索引部分则持有数据部分的指针。移动操作本质上就是拷贝了较小的索引部分,复用了较大的数据部分,因而比较轻快。
拿 std::vector 为例来说明:
:::tips
对于移动一个 vector 来说,我们只是复制了 vecotr 的索引部分,然后重用了vecotr 的数据部分
:::
代码示例 :
#include <iostream>#include <numeric>#include <vector>int main() {std::vector<int> vi(1024);std::iota(vi.begin(), vi.end(), 0);std::cout << "sizeof of vi: " << sizeof(vi) << '\n'; // 24std::cout << "sizeof of data: " << sizeof(int) * vi.size() << '\n'; // 4096std::cout << "address of vi: " << std::addressof(vi) << '\n'; // 0x7ffee9450bf0std::cout << "address of data: " << vi.data() << '\n'; // 0x7fc924008800auto vi1 = std::move(vi);std::cout << vi.size() << '\n'; // 0std::cout << "address of vi: " << std::addressof(vi1) << '\n'; // 0x7ffee7989bb8std::cout << "address of data: " << vi1.data() << '\n'; // 0x7fc924008800// the move action, just move the small part of vector// and reuse the large part as of datareturn 0;}
🐂 移动操作的特例
在 C++ 中移动操作一般来说是高效的,但也有一些情况例外,需要了解:
- 对象压根就不支持移动操作
- 移动操作并不比拷贝操作快
- 移动操作因异常安全问题而退回为拷贝操作
- 移动操作因类型不匹配而退回为拷贝操作,比如没法移动一个 const 对象
- 再次强调不要移动左值
尤其对于函数模板来说,传入的参数五花八门,各种参数对移动操作的支持情况可能都不一样,我们不能假定对象的移动操作就一定存在,并且高效,移动操作一定能完成。
代码示例 :
#include <array>#include <iostream>#include <memory>#include <string>#include <vector>struct X {X(const std::string &x = "hello") : x_(x){};X(const X &x) { std::cout << "calls the copy ctor\n"; }// isn’t declared noexceptX(X &&rhs) {std::cout << "calls move ctor\n";x_ = std::move(rhs.x_);}std::string x_;};struct NonMoveable {NonMoveable() = default;// copy ctor and copy assignment disabled the move operationsNonMoveable(const NonMoveable &rhs) { std::cout << "calls the copy ctor\n"; }NonMoveable &operator=(const NonMoveable &rhs) {std::cout << "calls the copy assignment\n";return *this;}};int main() {// case 1: no move operations// move ctor will becomes copy ctor// move assignment becomes copy assignmentNonMoveable x1{};NonMoveable x2(std::move(x1)); // calls the copy ctorx2 = NonMoveable{}; // calls the copy assignment// case 2: move not faster than copy// the whole std::array is in the stack// the move action is not as cheap as copy several pointers// need to move the moveable element one by oneint i = 4;std::array<std::string, 4> arrs{"a", "b", "c", "d"};std::cout << sizeof(arrs) << std::endl; // 96std::cout << std::addressof(i) << std::endl; // 0x7ffeedde1bf4std::cout << std::addressof(arrs) << std::endl; // 0x7ffeedde1b90std::cout << arrs.data() << std::endl; // 0x7ffeedde1b90// move the arrs content to new memory addressauto arrs1 = std::move(arrs);std::cout << std::addressof(arrs1) << std::endl; // 0x7ffee7565b18std::cout << arrs1.data() << std::endl; // 0x7ffeedde1b18// case 3: strong exception safety guarantee// X's move ctor isn’t declared noexcept// move_if_noexcept will becomes copy ctorstd::vector<X> vx;vx.reserve(16);X x3{}, x4{};vx.push_back(x3); // calls the copy ctorvx.push_back(std::move(x3)); // calls move ctorvx.push_back(std::move_if_noexcept(x4)); // calls the copy ctor// case 4: can not move const objectconst X x5{};X x6 = std::move(x5); // calls the copy ctor// case 5: lvalue// std::move a lvalue will surprise the call side code// the caller does not know the processX will move away x5auto processX = [](auto &&x) { return std::move(x); };X x7{"world"};X x8 = processX(x7); // calls move ctorstd::cout << std::boolalpha << x7.x_.empty() << std::endl; // truereturn 0;}
🐯 总结
- 不要假定对象的移动操作一定会发生,发生了也未必就高效
- 适合移动操作的地方优先使用移动操作,最起码还有拷贝操作兜底
