universal reference
If a variable or parameter is declared to have type T&& for some deduced type T, that variable or parameter is a universal reference.
如果一个变量或参数声明为T&&类型,且T为推导类型,则这个变量或者参数称为universal reference。如果被一个左值初始化,它就是一个左值;如果它被一个右值初始化,它就是一个右值。
引用折叠
引用折叠仅发生在模板函数中。规则如下:
完美转发
C++11引入了完美转发:在函数模板中,完全依照模板的参数类型(即保持参数的左值、右值特征),将参数传递给函数模板中调用的另外一个函数。通过std::forward实现。
场景
当我们需要将参数转发到参数分别为左值和右值的重载函数中时,如果没有universal reference,我们必须写两个模板参数
void process(int& a){};//1
void process(int&& a){};//2
template<class T>
void g(T& t)
{
process(t);
}
template<class T>
void g(T&& t)
{
process(t);//实际调用1,后面解决
}
由于引用折叠机制和universal reference的存在,我们可以合并两个模板:
void process(int& a){};//1
void process(int&& a){};//2
template<class T>
void g(T&& t)
{
process(t);//实际调用1,后面解决
}
这里还存在一个问题如果我们传入一个右值:
g(0);
实际调用的还是1函数,为什么?因为t是一个右值引用,但是t本身是个左值,因此调用的是1。
这时就需要std::forward出场了:
void process(int& a){};//1
void process(int&& a){};//2
template<class T>
void g(T&& t)
{
process(std::forward<T>(t));
}
此时g(0)调用的就是2处函数。
总结一下,万能引用方便了模板函数的定义,而完美转发解决了参数转发过程中右值变为左值的问题。
参考:
- https://zhuanlan.zhihu.com/p/351981942
- https://www.cnblogs.com/qicosmos/p/4325949.html#!comments
- https://blog.csdn.net/itworld123/article/details/110914744