🐭 std::move 的使用规则

std::move 应该只作用于右值引用,当需要多次使用该值的时候,应该只在最后一处使用 std::move。在编码的时候,尤其是在老代码后面添加新代码的时候,需要注意这种情况。另外,这个规则同样适用于按值返回的函数的返回值。
代码示例 :

  1. #include <iostream>
  2. struct X {
  3. X(std::string_view sv) : name_(sv) { std::cout << sv << '\n'; }
  4. // the parameter rhs is a lvalue
  5. // even though its type is rvalue reference to X
  6. X(X &&rhs) {
  7. std::cout << rhs.name_ << '\n'; // use the rhs 1st time
  8. name_ = std::move(rhs.name_); // use the rhs 2nd time
  9. std::cout << rhs.name_.empty() << '\n'; // true
  10. }
  11. std::string name_;
  12. };
  13. int main() {
  14. std::cout << std::boolalpha;
  15. // 1. std::move cast the ms1 to rvalue reference
  16. // 2. and then std::string's move ctor will move ms1's content to ms2
  17. std::string ms1{"hello"};
  18. std::string ms2 = std::move(ms1);
  19. std::cout << ms1.empty() << '\n'; // true
  20. X x1{"x1"};
  21. X x2(std::move(x1));
  22. // does not use the x1 any more
  23. X x3(X{"x3"}); // copy elision, calls the ctor directly
  24. return 0;
  25. }

🐂 std::forward 使用规则

std::forward 应该只作用于通用引用,当需要多次使用该值的时候,应该只在最后一处使用 std::forward。在编码的时候,尤其是在老代码后面添加新代码的时候,需要注意这种情况。另外,这个规则同样适用于按值返回的函数的返回值。
代码示例 :

  1. #include <iostream>
  2. struct X {
  3. X(std::string_view sv) : name_(sv) {}
  4. // forwarding (universal) reference
  5. template <typename T> void set_name(T &&name) {
  6. std::cout << name << '\n'; // use the rhs 1st time
  7. name_ = std::forward<T>(name); // use the rhs 2nd time
  8. // name_ = std::forward<std::string>(name); // wrong usage, never do this
  9. std::cout << name.empty() << '\n'; // true / false
  10. }
  11. std::string name_;
  12. };
  13. int main() {
  14. std::cout << std::boolalpha;
  15. // 1. pass rvalue to set_name
  16. using std::string_literals::operator""s;
  17. X x4{"x4"};
  18. x4.set_name("hello"s);
  19. // 2. pass lvalue to set_name
  20. std::string s{"world"};
  21. x4.set_name(s);
  22. std::cout << s.empty() << '\n'; // false
  23. return 0;
  24. }

:::tips

  1. std::forward 涉及到类型推导,而 std::move 不涉及类型推导
  2. std::forward 的类型参数 U 必须为模板参数 T,而不能是具体的类型,否则就相当于 std::move
  3. std::move 作用于右值,std::forward 作用于通用引用,不要张冠李戴
  4. 对于按值返回的函数,对于右值引用返回值执行 std::move, 对于通用引用返回值执行 std::forward ::: std::move 与 std::forward 张冠李戴:
    perfect_forwarding.png

    🐯 RVO 与 NRVO

    返回值优化,避免创建临时对象,这个技术比较反直觉,习惯就好。返回值优化使得设计按值返回的函数不再被认为是低效的,这个技术可以让我们不用设计带输出参数的函数。
    RVO: 无名返回值优化,C++17 对此有增强
    NRVO:具名返回值优化
    代码示例 : ```cpp

    include

    include

    include

struct X { X() { std::cout << “X::X\n”; } X(const X &) { std::cout << “X(const X&)\n”; } X(X &&) { std::cout << “X(X&&)\n”; } ~X() { std::cout << “~X\n”; } }; X Create0() { // return value optimisation (RVO) return X(); // std::move and std::forward will preevents RVO // return std::move(X()); // return std::forward(X()); } X Create1() { // named return value optimisation (NRVO) X x; return x; // std::move and std::forward will preevents NRVO // return std::move(x); // return std::forward(x); }

struct NonMoveable { NonMoveable(int v) : v(v) {} NonMoveable(const NonMoveable &) = delete; // non-copyable NonMoveable(NonMoveable &&) = delete; // non-movable std::array arr; int v_; }; NonMoveable make(int val) { // C++17 copy elision works only for temporary objects, not for NRVO if (val > 0) return NonMoveable(val); return NonMoveable(-val); }

int main() { // RVO / NRVO construct the X object directly in memory of x0 or x1 // No copy, no move { // RVO auto x0 = Create0(); } std::cout << std::string(4, ‘=’) << std::endl; { // NRVO auto x1 = Create1(); } std::cout << std::string(4, ‘=’) << std::endl;

{ // construct the non-movable/non-copyable object (C++17) // No copy, no move auto obj = make(-1024); }

return 0; } ```

🐰 总结

  1. 对右值引用,在最后一次使用的地方 std::move 右值引用
  2. 对通用引用,在最后一次使用的地方 std::forward 通用引用
  3. 不要对临时变量乱用 std::move 与 std::forward,进而无意中阻止了返回值优化与拷贝消除