🐭 形参、实参与参数修饰符
对于下面的函数模板伪代码:
template <typename T> void f(paramType param);// deduce two types T and paramType from exprf(expr);
形参 (param)
函数声明时候的「形式参数」,总是一个左值。
实参 (expr)
函数调用时候的「实际参数」,用来初始化形参。
参数修饰符 (T, paramType)
- const、volatile、reference (cvref) 修饰符
- 指针修饰符
- 数组修饰符
- 复合修饰符,就是 1,2,3 修饰符的组合
代码示例 :
#include <complex>// case 42: universal(forwarding) reference qualifiertemplate <typename T> decltype(auto) fwd0(T &¶m){}; // template forwarding referencedecltype(auto) fwd1 = [](auto &¶m) {}; // lambda forwarding referenceint main(void) {using CT = std::complex<double>;// case 1: no qualifierCT p1{};// case 2: const qualifierconst CT p2{p1};// case 3: volatile qualifiervolatile CT p3{p1};// case 40: reference qualifierCT &p400 = p1; // lvalue referenceCT &&p401 = CT{}; // rvalue reference// case 41: const lvalue reference qualifierconst CT &p410 = p1; // const lvalue reference bind to lvalueconst CT &p411 = CT{}; // const lvalue reference bind to rvalue, lifetime extension// case 42: universal reference qualifierauto &&p420 = p1; // auto&& bind to lvalueauto &&p421 = CT{}; // auto&& bind to rvalue, lifetime extension// case 5: pointer qualifierCT *p50 = nullptr;// '*' left side 'const' means can not modify 'p1' through 'p51'// '*' right side 'const' means can not modify 'p51' to point to other object than 'p1'// 'p51' and 'p52' are equivalentconst CT *const p51 = &p1;CT const *const p52 = &p1;// case 6: array qualifierCT p6[2] = {p1, p1};return 0;}
🐂 模板类型推导
模板类型推导根据形参的参数类型,可以分为下面三种情况:
- 形参为一般引用类型 (左值引用或者右值引用) 或者指针类型
- 形参为通用 (转发) 引用类型
- 形参为值类型
🐯 形参为一般引用类型或者指针类型
伪代码: ```cpp template// param is a lvaule reference void f(T ¶m);
template
template
// deduce type of T from expr f(expr);
这种情况下只要 T 的类型确定了,param 的类型也就确定了。<br />推导规则:<br />第一步:如果 expr 是引用类型,忽略掉引用修饰部分<br />第二步:expr 与形参进行类型匹配进而确定 T 的类型,类型匹配总是趋向于更严格的类型:::tips类型的严格性:有修饰符的比没修饰符的更严格,修饰符多的比修饰符少的更严格:::**代码示例** :```cpp#include <iostream>// param is a lvaule reference typetemplate <typename T> void f1(T ¶m) {if (std::is_same_v<T, int>) {std::cout << "T = int\n";} else if (std::is_same_v<T, const int>) {std::cout << "T = const int\n";}};// param is a pointer typetemplate <typename T> void f2(T *param) {if (std::is_same_v<T, int>) {std::cout << "T = int\n";} else if (std::is_same_v<T, const int>) {std::cout << "T = const int\n";}}int main(void) {int x = 42;int &rx = x;const int cx = x;const int &crx = cx;f1(rx); // T is `int`, param's type is `int&`f1(crx); // T is `const int`, param's type is `const int&`f2(&x); // T is `int`, param's type is `int*`f2(&cx); // T is `const int`, param's type is `const int*`return 0;}
🐰 形参为通用引用类型
伪代码:
template <typename T>// param is a universal referencevoid f(T &¶m);template <typename T>// param is a rvalue referencevoid g(const T &¶m);// X is a universal referenceauto &&X = expr;// Y is a rvalue referenceconst auto &&Y = expr;// deduce type of T and param from exprf(expr);
:::tips
注意通用引用的两种代码形式,不能有任何修饰符,哪怕是一个 const 修饰符
通用引用往往与完美转发一起工作,更严格的说应该叫转发引用
:::
推导规则:
- 当 expr 是一个左值的时候,T 和 param 都推导为左值引用类型,应用了引用折叠规则
- 当 expr 是一个右值的时候,T 推导为值类型,param 推导为右值引用类型
代码示例 :
#include <iostream>template <typename T> void f(T &¶m) {if (std::is_same_v<T, int>) {std::cout << "T = int\n";} else if (std::is_same_v<T, const int>) {std::cout << "T = const int\n";} else if (std::is_same_v<T, int &>) {std::cout << "T = int&\n";} else if (std::is_same_v<T, const int &>) {std::cout << "T = const int&\n";}if (std::is_same_v<decltype(param), int &>) {std::cout << "param = int&\n";} else if (std::is_same_v<decltype(param), const int &>) {std::cout << "param = const int&\n";} else if (std::is_same_v<decltype(param), int &&>) {std::cout << "param = int&&\n";} else if (std::is_same_v<decltype(param), const int &&>) {std::cout << "param = const int&&\n";}}int main(void) {int x = 42;const int cx = x;f(x); // pass in lvalue, T and param are both `int&`f(cx); // pass in const lvalue, T and param are both `const int&`f(std::move(x)); // pass in rvalue, T is `int` and param is `int&&`f(std::move(cx)); // pass in const rvalue, T is `const int` and param is `const int&&`return 0;}
🐲 形参为值类型
伪代码:
template<typename T>// param is now passed by valuevoid f(T param);// deduce type of T from exprf(expr);
这种情况下只要 T 的类型确定了,param 的类型也就确定了。
推导规则:
第一步:忽略掉 expr 的 cvref 修饰符
第二步:expr 与形参进行类型匹配进而确定 T 的类型,类型匹配总是趋向于更严格的类型
代码示例 :
#include <iostream>template <typename T> void f(T param) {if (std::is_same_v<T, int>) {std::cout << "T = int\n";} else if (std::is_same_v<T, int *>) {std::cout << "T = int*\n";} else if (std::is_same_v<T, const int *>) {std::cout << "T = const int*\n";}}template <typename T, std::size_t N>constexpr std::size_t array_size(T (&)[N]) noexcept {return N;}int main(void) {int x = 42;const volatile int &cvrefx = x;const int *const cpcx = &x;int ax[2] = {x, x};// remove cvrefx's cvref qualifier, T and param are both `int`f(cvrefx);// remove cpcx's const qualifier (the second const), T and param are both `const int*`f(cpcx);// array reduced to a pointer,T and param are both `int*`f(ax);std::cout << array_size(ax) << '\n'; // 2return 0;}
:::tips 特殊的数组:
- 数组 T[N] 按值传递的时候,退化为 T*,丢失了长度信息
- 数组 T[N] 按引用传递的时候,类型为 T (&)[N],保留了长度信息
指针的双重 const 修饰:
const T * const cpct = nullptr;
