🐭 形参、实参与参数修饰符

对于下面的函数模板伪代码:

  1. template <typename T> void f(paramType param);
  2. // deduce two types T and paramType from expr
  3. f(expr);

形参 (param)
函数声明时候的「形式参数」,总是一个左值。
实参 (expr)
函数调用时候的「实际参数」,用来初始化形参。
参数修饰符 (T, paramType)

  1. const、volatile、reference (cvref) 修饰符
  2. 指针修饰符
  3. 数组修饰符
  4. 复合修饰符,就是 1,2,3 修饰符的组合

代码示例 :

  1. #include <complex>
  2. // case 42: universal(forwarding) reference qualifier
  3. template <typename T> decltype(auto) fwd0(T &&param){}; // template forwarding reference
  4. decltype(auto) fwd1 = [](auto &&param) {}; // lambda forwarding reference
  5. int main(void) {
  6. using CT = std::complex<double>;
  7. // case 1: no qualifier
  8. CT p1{};
  9. // case 2: const qualifier
  10. const CT p2{p1};
  11. // case 3: volatile qualifier
  12. volatile CT p3{p1};
  13. // case 40: reference qualifier
  14. CT &p400 = p1; // lvalue reference
  15. CT &&p401 = CT{}; // rvalue reference
  16. // case 41: const lvalue reference qualifier
  17. const CT &p410 = p1; // const lvalue reference bind to lvalue
  18. const CT &p411 = CT{}; // const lvalue reference bind to rvalue, lifetime extension
  19. // case 42: universal reference qualifier
  20. auto &&p420 = p1; // auto&& bind to lvalue
  21. auto &&p421 = CT{}; // auto&& bind to rvalue, lifetime extension
  22. // case 5: pointer qualifier
  23. CT *p50 = nullptr;
  24. // '*' left side 'const' means can not modify 'p1' through 'p51'
  25. // '*' right side 'const' means can not modify 'p51' to point to other object than 'p1'
  26. // 'p51' and 'p52' are equivalent
  27. const CT *const p51 = &p1;
  28. CT const *const p52 = &p1;
  29. // case 6: array qualifier
  30. CT p6[2] = {p1, p1};
  31. return 0;
  32. }

🐂 模板类型推导

模板类型推导根据形参的参数类型,可以分为下面三种情况:

  1. 形参为一般引用类型 (左值引用或者右值引用) 或者指针类型
  2. 形参为通用 (转发) 引用类型
  3. 形参为值类型

    🐯 形参为一般引用类型或者指针类型

    伪代码: ```cpp template // param is a lvaule reference void f(T &param);

template // param is a rvaule reference void f(const T &&param);

template // param is a pointer void f(T *param);

// deduce type of T from expr f(expr);

  1. 这种情况下只要 T 的类型确定了,param 的类型也就确定了。<br />推导规则:<br />第一步:如果 expr 是引用类型,忽略掉引用修饰部分<br />第二步:expr 与形参进行类型匹配进而确定 T 的类型,类型匹配总是趋向于更严格的类型
  2. :::tips
  3. 类型的严格性:有修饰符的比没修饰符的更严格,修饰符多的比修饰符少的更严格
  4. :::
  5. **代码示例** :
  6. ```cpp
  7. #include <iostream>
  8. // param is a lvaule reference type
  9. template <typename T> void f1(T &param) {
  10. if (std::is_same_v<T, int>) {
  11. std::cout << "T = int\n";
  12. } else if (std::is_same_v<T, const int>) {
  13. std::cout << "T = const int\n";
  14. }
  15. };
  16. // param is a pointer type
  17. template <typename T> void f2(T *param) {
  18. if (std::is_same_v<T, int>) {
  19. std::cout << "T = int\n";
  20. } else if (std::is_same_v<T, const int>) {
  21. std::cout << "T = const int\n";
  22. }
  23. }
  24. int main(void) {
  25. int x = 42;
  26. int &rx = x;
  27. const int cx = x;
  28. const int &crx = cx;
  29. f1(rx); // T is `int`, param's type is `int&`
  30. f1(crx); // T is `const int`, param's type is `const int&`
  31. f2(&x); // T is `int`, param's type is `int*`
  32. f2(&cx); // T is `const int`, param's type is `const int*`
  33. return 0;
  34. }

🐰 形参为通用引用类型

伪代码:

  1. template <typename T>
  2. // param is a universal reference
  3. void f(T &&param);
  4. template <typename T>
  5. // param is a rvalue reference
  6. void g(const T &&param);
  7. // X is a universal reference
  8. auto &&X = expr;
  9. // Y is a rvalue reference
  10. const auto &&Y = expr;
  11. // deduce type of T and param from expr
  12. f(expr);

:::tips 注意通用引用的两种代码形式,不能有任何修饰符,哪怕是一个 const 修饰符
通用引用往往与完美转发一起工作,更严格的说应该叫转发引用 ::: 推导规则:

  1. 当 expr 是一个左值的时候,T 和 param 都推导为左值引用类型,应用了引用折叠规则
  2. 当 expr 是一个右值的时候,T 推导为值类型,param 推导为右值引用类型

代码示例 :

  1. #include <iostream>
  2. template <typename T> void f(T &&param) {
  3. if (std::is_same_v<T, int>) {
  4. std::cout << "T = int\n";
  5. } else if (std::is_same_v<T, const int>) {
  6. std::cout << "T = const int\n";
  7. } else if (std::is_same_v<T, int &>) {
  8. std::cout << "T = int&\n";
  9. } else if (std::is_same_v<T, const int &>) {
  10. std::cout << "T = const int&\n";
  11. }
  12. if (std::is_same_v<decltype(param), int &>) {
  13. std::cout << "param = int&\n";
  14. } else if (std::is_same_v<decltype(param), const int &>) {
  15. std::cout << "param = const int&\n";
  16. } else if (std::is_same_v<decltype(param), int &&>) {
  17. std::cout << "param = int&&\n";
  18. } else if (std::is_same_v<decltype(param), const int &&>) {
  19. std::cout << "param = const int&&\n";
  20. }
  21. }
  22. int main(void) {
  23. int x = 42;
  24. const int cx = x;
  25. f(x); // pass in lvalue, T and param are both `int&`
  26. f(cx); // pass in const lvalue, T and param are both `const int&`
  27. f(std::move(x)); // pass in rvalue, T is `int` and param is `int&&`
  28. f(std::move(cx)); // pass in const rvalue, T is `const int` and param is `const int&&`
  29. return 0;
  30. }

🐲 形参为值类型

伪代码:

  1. template<typename T>
  2. // param is now passed by value
  3. void f(T param);
  4. // deduce type of T from expr
  5. f(expr);

这种情况下只要 T 的类型确定了,param 的类型也就确定了。
推导规则:
第一步:忽略掉 expr 的 cvref 修饰符
第二步:expr 与形参进行类型匹配进而确定 T 的类型,类型匹配总是趋向于更严格的类型
代码示例 :

  1. #include <iostream>
  2. template <typename T> void f(T param) {
  3. if (std::is_same_v<T, int>) {
  4. std::cout << "T = int\n";
  5. } else if (std::is_same_v<T, int *>) {
  6. std::cout << "T = int*\n";
  7. } else if (std::is_same_v<T, const int *>) {
  8. std::cout << "T = const int*\n";
  9. }
  10. }
  11. template <typename T, std::size_t N>
  12. constexpr std::size_t array_size(T (&)[N]) noexcept {
  13. return N;
  14. }
  15. int main(void) {
  16. int x = 42;
  17. const volatile int &cvrefx = x;
  18. const int *const cpcx = &x;
  19. int ax[2] = {x, x};
  20. // remove cvrefx's cvref qualifier, T and param are both `int`
  21. f(cvrefx);
  22. // remove cpcx's const qualifier (the second const), T and param are both `const int*`
  23. f(cpcx);
  24. // array reduced to a pointer,T and param are both `int*`
  25. f(ax);
  26. std::cout << array_size(ax) << '\n'; // 2
  27. return 0;
  28. }

:::tips 特殊的数组:

  1. 数组 T[N] 按值传递的时候,退化为 T*,丢失了长度信息
  2. 数组 T[N] 按引用传递的时候,类型为 T (&)[N],保留了长度信息

指针的双重 const 修饰:
const T * const cpct = nullptr;

  1. 星号左侧的 const,是修饰指针所指向的对象,表示不能通过该指针来修改其所指向的对象的值
  2. 星号右侧的 const,是修饰指针自己,表示不能修改该指针进而使其指向别的对象 :::

    🐍 总结

  3. 所有推导规则都会忽略掉实参的引用修饰符

  4. 按值传参还会进一步地忽略掉实参的 const 与 volatile (cv) 修饰符
  5. 通用引用可以接收左值,也可以接收右值,接收左值时会发生引用折叠
  6. 数组和函数名字按值传递时会退化为指针,它们也可以按引用传递