问题描述
在此示例中,斯科特·迈耶斯(Scott Meyers)明确阐明了右值引用和转发引用之间的区别:
The distinction between rvalue references and forwarding references was made clear enough in this example by Scott Meyers:
Widget&& var1 = someWidget; // here, "&&" means rvalue reference (1)
auto&& var2 = var1; // here, "&&" does not mean rvalue reference (2)
template<typename T>
void f(std::vector<T>&& param); // here, "&&" means rvalue reference (3)
template<typename T>
void f(T&& param); // here, "&&"does not mean rvalue reference (4)
本质上,当我们有一个可推论的上下文时就发生了区分,因此情况(3)明确指出我们有一个vector<...>&&
,而要推论情况(4)的T
,并且(在应用参考折叠规则之后)按值类别"分类.
Essentially the distinction happens when we have a deducible context, hence case (3) explicitly states that we have a vector<...>&&
whereas the T
in case (4) is to be deduced and (after applying reference collapsing rules) categorized in terms of "value category".
但是,模式匹配稍微复杂一点会怎样?以下面的情况为例:
template <template <class...> class Tuple, class... Ts>
void f(Tuple<Ts...>&& arg)
{
}
&&
在这里是什么意思?
What does &&
mean here ?
推荐答案
在最后一个示例中,arg
是右值引用.
In your last example, arg
is an rvalue reference.
和Tuple<Ts...>
不是模板参数.
(来自[temp.deduct.call]的引用.)
(Citation from [temp.deduct.call].)
这篇关于这是转发参考吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!