解決什么問題
在介紹完美轉(zhuǎn)發(fā)之前,我們先來看下面的一段代碼。
void foo(const shape&)
{
puts("foo(const shape&)");
}
void foo(shape&&)
{
puts("foo(shape&&)");
}
void bar(const shape& s)
{
puts("bar(const shape&)");
foo(s);
}
void bar(shape&& s)
{
puts("bar(shape&&)");
foo(s);
}
int main()
{
bar(circle());
}
我們猜下,上面會(huì)輸出什么。
===============================留你思考=================================
circle()會(huì)生成一個(gè)右值,所以會(huì)調(diào)用bar(shape&&),這個(gè)時(shí)候參數(shù)s變?yōu)榱俗笾?,所以?huì)調(diào)用foo(const shape&),所以以上的輸出是:
bar(shape&&)
foo(const shape&)
如果我們希望保持bar函數(shù)收到的右值引用,我們就要變?yōu)橄旅娴恼{(diào)用方式:
void bar(shape&& s)
{
puts("bar(shape&&)");
foo(std::move(s));
}
可以看到了,bar函數(shù)為了保證在轉(zhuǎn)發(fā)參數(shù)的過程中,保持參數(shù)的值類別:左值的仍然是左值,右值的仍然是右值,不得不重載兩個(gè)bar函數(shù),那有什么簡單的方法能夠?qū)崿F(xiàn)呢?
這就有了c++標(biāo)準(zhǔn)庫中的std::forward,即轉(zhuǎn)發(fā)引用(forwarding reference),也叫做萬能引用(universal reference),具體到實(shí)現(xiàn)上來說就是:
template<typename T>
void bar(T &&s) {
foo(std::forward<T>(s));
}
此時(shí)再看下面的輸出:
circle temp;
bar(temp);
bar(circle());
大家應(yīng)該能夠很容易回答上來了。
總結(jié)
本文介紹了c++中轉(zhuǎn)發(fā)引用的使用場景:保證在轉(zhuǎn)發(fā)參數(shù)的過程中,保持參數(shù)的值類別:左值的仍然是左值,右值的仍然是右值。
最后一段代碼的輸出,歡迎留言的。