🐭 完美转发

完美转发指的是将一个变量,从一个函数传递到另外一个函数,再传到更内层的函数等等,整个传递过程中保持变量的类型不变,是通用引用的应用场景。之所以称为完美转发是因为:

  1. 相对于按值传递,传的是引用而不是副本
  2. 相对于按指针传递,更安全
  3. 能保留对象的 cvref 类型属性修饰
  4. 完美转发能转发任意类型

代码示例 :

  1. #include <iostream>
  2. class X {};
  3. void func(X &&x) { std::cout << "calls func(X &&x)\n"; }
  4. void func(X &x) { std::cout << "calls func(X &x)\n"; }
  5. void func(const X &x) { std::cout << "calls func(const X &x)\n"; }
  6. void func(volatile X &x) { std::cout << "calls func(volatile X &x)\n"; }
  7. auto fwd1 = [](auto &&... params) {
  8. // accept any type of arguments and forward them to func
  9. return func(std::forward<decltype(params)>(params)...);
  10. };
  11. template <typename... Ts> auto fwd2(Ts &&... params) {
  12. return func(std::forward<Ts>(params)...);
  13. }
  14. int main() {
  15. // fwd can keeps the passed in value's cvr qualifier
  16. X x00{}, x01{};
  17. X x1{};
  18. const X x2{};
  19. volatile X x3{};
  20. fwd1(std::move(x00)); // calls func(X &&x)
  21. fwd1(x1); // calls func(X &x)
  22. fwd1(x2); // calls func(const X &x)
  23. fwd1(x3); // calls func(volatile X &x)
  24. fwd2(std::move(x01)); // calls func(X &&x)
  25. fwd2(x1); // calls func(X &x)
  26. fwd2(x2); // calls func(const X &x)
  27. fwd2(x3); // calls func(volatile X &x)
  28. return 0;
  29. }

🐂 完美转发的局限性

完美转发大部分时候都能很好的工作,下列类型不能很好的与完美转发一起工作:

  1. 括号初始化物
  2. 0, NULL 作为空指针
  3. 没有定义的 static const 整数数据成员
  4. 重载函数与重载模板
  5. 绑定位域

代码示例 :

  1. #include <iostream>
  2. #include <vector>
  3. void func(const std::vector<int> &v) {
  4. std::cout << "v's size is: " << v.size() << std::endl;
  5. };
  6. void func(const char *cs) {
  7. if (cs == nullptr) {
  8. std::cout << "cs is a nullptr" << std::endl;
  9. }
  10. }
  11. template <typename... Ts> void fwd(Ts &&... param) {
  12. // accept any type of arguments and forward them to func
  13. func(std::forward<Ts>(param)...);
  14. }
  15. int main() {
  16. // case 1: braced initializers
  17. // auto can deduce std::initializer_list
  18. // template never deduce std::initializer_list
  19. func({1, 2, 3}); // v's size is: 3
  20. // fwd({1, 2, 3}); // doesn’t compile
  21. auto v = {1, 2, 3}; // workaround
  22. fwd(v); // v's size is: 3
  23. // case 2: 0 or NULL as null pointers
  24. func(0), func(NULL), fwd(nullptr); // cs is a nullptr (3 times)
  25. // fwd(0); // 0 deduce to int
  26. // fwd(NULL); // NULL deduce to long
  27. // case 3: declaration only integral static const data members
  28. // declaration only integral static const data members
  29. // does not has a memory address, so can not apply reference or pointer on them
  30. // an edge case, no example
  31. // case 4: overloaded function names and template names
  32. // function template does not know which overloaded function to chosen
  33. // need to passed the right full function type
  34. // an edge case, no example
  35. // case 5: bitfields
  36. // more or less the same as case 3
  37. // can not apply reference or pointer on bitfields
  38. // an edge case, no example
  39. return 0;
  40. }

:::tips 完美转发失败的情形可分为三类:

  1. 无法进行有效的类型推导,比如 1 和 4
  2. 类型推导结果错误,比如 2
  3. 无法获取变量的地址,比如 3 和 5 :::

    🐯 总结

  4. 完美转发并不完美,有 5 种情况会导致完美转发失败

  5. 类型推导失败与类型推导错误,会导致完美转发失败,尤其要注意后者