🐭 右值引用与通用引用

右值引用:
现代 C++ 新引入的一种引用类型,大概语法形式为 T&&,右值引用只能绑定到一个右值,一般与 std::move 一起工作。
通用引用:
一种比较特殊的引用类型,大概语法形式也是 T&&,它会根据初始化物的类型进行类型推导,可以绑定到一个左值也可以绑定到一个右值,一般与 std::forward 一起工作,也叫转发引用。
代码示例 :

  1. #include <iostream>
  2. struct X {};
  3. // T&& as rvalue reference
  4. // to identify objects that may be moved from
  5. void func1(X &&x){}; // rvalue reference
  6. template <typename T> void func2(std::vector<T> &&arg); // rvalue reference
  7. template <typename T> void func3(const T &&arg); // rvalue reference
  8. X &&x1 = X{}; // rvalue reference
  9. // T&& as a universal (forwarding) reference
  10. // must be type deduction, arise in two contexts
  11. // 1. for function template parameters
  12. // 2. auto&& declarations, including lambda case
  13. template <typename T> void func3(T &&arg); // forwarding reference
  14. auto &&x2 = x1; // universal reference
  15. // auto&& in lambda as forwarding reference
  16. // func is a universal reference that can be bound to any callable object,
  17. // args is zero or more universal references that can be bound to any number
  18. // of objects of arbitrary types
  19. decltype(auto) funcInvocation = [](auto &&func, auto &&... args) {
  20. return std::forward<decltype(func)>(func)(std::forward<decltype(args)>(args)...);
  21. };
  22. // T&& as a universal (forwarding) reference
  23. // can bind to lvalue and rvalue (can bind to virtually anything)
  24. // 1. bind to lvalue forms a lvalue reference
  25. // 2. bind to rvalue forms a rvalue reference
  26. template <typename T, std::enable_if_t<std::is_same_v<std::decay_t<T>, X>, int> = 0>
  27. void func4(T &&arg) {
  28. if constexpr (std::is_same_v<decltype(arg), X &>) {
  29. std::cout << "arg = X&\n";
  30. } else if constexpr (std::is_same_v<decltype(arg), const X &>) {
  31. std::cout << "arg = const X &\n";
  32. } else if constexpr (std::is_same_v<decltype(arg), X &&>) {
  33. std::cout << "arg = X&&\n";
  34. } else if constexpr (std::is_same_v<decltype(arg), const X &&>) {
  35. std::cout << "arg = const X &&\n";
  36. } else {
  37. std::cout << "unknown type\n";
  38. }
  39. }
  40. int main() {
  41. std::cout << std::boolalpha;
  42. // T&& bind to lvalue and const lvalue
  43. X x1{};
  44. const X x2{};
  45. func4(x1); // arg = X&
  46. func4(x2); // arg = const X &
  47. // T&& bind to rvalue and const rvalue
  48. func4(X{}); // arg = X&&
  49. func4(std::move(x1)); // arg = X&&
  50. func4(std::move(x2)); // arg = const X &&
  51. // auto&& bind to lvalue reference
  52. X x3{};
  53. auto &&x31 = x3;
  54. std::cout << std::is_same_v<decltype(x31), X &> << std::endl; // true
  55. // auto&& bind to rvalue reference
  56. auto &&x4 = X{};
  57. std::cout << std::is_same_v<decltype(x4), X &&> << std::endl; // true
  58. // auto&& in lambda
  59. auto sum = [](auto &&... args) { return (args + ...); };
  60. std::cout << funcInvocation(sum, 1, 2, 3, 4, 5, 6) << std::endl; // 21
  61. return 0;
  62. }

应用场景:
rvalue_vs_forwarding.png

🐂 引用类型识别

左值引用:
语法形式为 T&,比较好识别
通用引用 (转发引用):
语法形式为 T&&,主要就两个应用场景,并且必须有类型推导过程

  1. 形如 T&& 的函数模板参数
  2. 形如 auto&& 的 auto 变量声明,包括 lambda 形参

右值引用:
语法形式为 T&&,除通用引用之外的都是右值引用

左值引用,只能绑定到左值,但是 const T& 常量左值引用却既可以绑定到左值,也可以绑定到右值,但结果都是常量左值引用。
右值引用只能绑定到右值。
通用引用根据其初始化物的类型,可以绑定到一个左值,也可以绑定到一个右值,比较灵活,一般用来最大化函数参数的普适性。

🐯 总结

  1. 目前通用引用就只有两种形式,很好识别,识别好了之后再识别右值引用
  2. 通用引用与 const T& 常量左值引用,都是既可以绑定到左值,也可以绑定到右值,但它们的结果不一样