函数模板可以将自己的参数完美转发给内部调用的其他函数,完美指的是不仅能够准确地转发参数的值,还能保证被转发参数的左右值属性不变。
//// Created by Administrator on 2022/2/21.///** 完美转发*/#include<bits/stdc++.h>using namespace std;void otherdef(int &t) {cout<<"lvalue"<<endl;}void otherdef(const int &t) {cout<<"rvalue"<<endl;}template<typename T>void myfunction(T && t) {otherdef(forward<T>(t));}int main() {myfunction(5);int x = 1;myfunction(x);return 0;}
