🐭 移动操作的本质

移动语义可以说是现代 C++ 中最重要的特性之一,它解决了 C++ 中一直被人诟病的临时对象拷贝问题,让很多昂贵的拷贝操作变成了简单的移动操作,大大提升了 C++ 的性能。对老于代码,哪怕只是重新使用现代 C++ 编译器编译一遍都会有性能上的提升。
移动操作的本质:
C++ 中的大对象,比如说容器,在逻辑上一般由两个组成部分:比较小的「索引」部分与比较大的数据部分。数据部分一般存放在堆上,而索引部分则持有数据部分的指针。移动操作本质上就是拷贝了较小的索引部分,复用了较大的数据部分,因而比较轻快。
拿 std::vector 为例来说明: :::tips 对于移动一个 vector 来说,我们只是复制了 vecotr 的索引部分,然后重用了vecotr 的数据部分 ::: 代码示例 :

  1. #include <iostream>
  2. #include <numeric>
  3. #include <vector>
  4. int main() {
  5. std::vector<int> vi(1024);
  6. std::iota(vi.begin(), vi.end(), 0);
  7. std::cout << "sizeof of vi: " << sizeof(vi) << '\n'; // 24
  8. std::cout << "sizeof of data: " << sizeof(int) * vi.size() << '\n'; // 4096
  9. std::cout << "address of vi: " << std::addressof(vi) << '\n'; // 0x7ffee9450bf0
  10. std::cout << "address of data: " << vi.data() << '\n'; // 0x7fc924008800
  11. auto vi1 = std::move(vi);
  12. std::cout << vi.size() << '\n'; // 0
  13. std::cout << "address of vi: " << std::addressof(vi1) << '\n'; // 0x7ffee7989bb8
  14. std::cout << "address of data: " << vi1.data() << '\n'; // 0x7fc924008800
  15. // the move action, just move the small part of vector
  16. // and reuse the large part as of data
  17. return 0;
  18. }

🐂 移动操作的特例

在 C++ 中移动操作一般来说是高效的,但也有一些情况例外,需要了解:

  1. 对象压根就不支持移动操作
  2. 移动操作并不比拷贝操作快
  3. 移动操作因异常安全问题而退回为拷贝操作
  4. 移动操作因类型不匹配而退回为拷贝操作,比如没法移动一个 const 对象
  5. 再次强调不要移动左值

尤其对于函数模板来说,传入的参数五花八门,各种参数对移动操作的支持情况可能都不一样,我们不能假定对象的移动操作就一定存在,并且高效,移动操作一定能完成。
代码示例 :

  1. #include <array>
  2. #include <iostream>
  3. #include <memory>
  4. #include <string>
  5. #include <vector>
  6. struct X {
  7. X(const std::string &x = "hello") : x_(x){};
  8. X(const X &x) { std::cout << "calls the copy ctor\n"; }
  9. // isn’t declared noexcept
  10. X(X &&rhs) {
  11. std::cout << "calls move ctor\n";
  12. x_ = std::move(rhs.x_);
  13. }
  14. std::string x_;
  15. };
  16. struct NonMoveable {
  17. NonMoveable() = default;
  18. // copy ctor and copy assignment disabled the move operations
  19. NonMoveable(const NonMoveable &rhs) { std::cout << "calls the copy ctor\n"; }
  20. NonMoveable &operator=(const NonMoveable &rhs) {
  21. std::cout << "calls the copy assignment\n";
  22. return *this;
  23. }
  24. };
  25. int main() {
  26. // case 1: no move operations
  27. // move ctor will becomes copy ctor
  28. // move assignment becomes copy assignment
  29. NonMoveable x1{};
  30. NonMoveable x2(std::move(x1)); // calls the copy ctor
  31. x2 = NonMoveable{}; // calls the copy assignment
  32. // case 2: move not faster than copy
  33. // the whole std::array is in the stack
  34. // the move action is not as cheap as copy several pointers
  35. // need to move the moveable element one by one
  36. int i = 4;
  37. std::array<std::string, 4> arrs{"a", "b", "c", "d"};
  38. std::cout << sizeof(arrs) << std::endl; // 96
  39. std::cout << std::addressof(i) << std::endl; // 0x7ffeedde1bf4
  40. std::cout << std::addressof(arrs) << std::endl; // 0x7ffeedde1b90
  41. std::cout << arrs.data() << std::endl; // 0x7ffeedde1b90
  42. // move the arrs content to new memory address
  43. auto arrs1 = std::move(arrs);
  44. std::cout << std::addressof(arrs1) << std::endl; // 0x7ffee7565b18
  45. std::cout << arrs1.data() << std::endl; // 0x7ffeedde1b18
  46. // case 3: strong exception safety guarantee
  47. // X's move ctor isn’t declared noexcept
  48. // move_if_noexcept will becomes copy ctor
  49. std::vector<X> vx;
  50. vx.reserve(16);
  51. X x3{}, x4{};
  52. vx.push_back(x3); // calls the copy ctor
  53. vx.push_back(std::move(x3)); // calls move ctor
  54. vx.push_back(std::move_if_noexcept(x4)); // calls the copy ctor
  55. // case 4: can not move const object
  56. const X x5{};
  57. X x6 = std::move(x5); // calls the copy ctor
  58. // case 5: lvalue
  59. // std::move a lvalue will surprise the call side code
  60. // the caller does not know the processX will move away x5
  61. auto processX = [](auto &&x) { return std::move(x); };
  62. X x7{"world"};
  63. X x8 = processX(x7); // calls move ctor
  64. std::cout << std::boolalpha << x7.x_.empty() << std::endl; // true
  65. return 0;
  66. }

🐯 总结

  1. 不要假定对象的移动操作一定会发生,发生了也未必就高效
  2. 适合移动操作的地方优先使用移动操作,最起码还有拷贝操作兜底