一旦确定了初始值设定项的类型,编译器就会使用从函数调用(参见模板参数推导#其他上下文获取详细信息)中的模板参数推导规则来确定将替换关键字auto
的类型。关键字auto
可能伴随有修饰符,例如const
或&
,它们将参与类型推演。
例如,在一个假想的
template template<class U>
void f(const U& u)
给出
const auto& i = expr;
类型的i
正是参数u
的类型如果函数调用f(expr)
编译的。
一般情况下,可以认为如下。
template template<class U>
void f(paramtype u)
因此,auto&&
可以根据初始化推断无论是作为一个左值参考或右值参考。
在你的情况下,虚模板看起来像
template template<class U>
void f(U&& var2){}
f(var1)
这里,var1
被命名为正被当作左值右值,所以var2
会推导出左值。
请看下面的例子:
auto&& var2 = widget() ; //var2 is rvalue reference here .
int x=10;
const int cx=10;
auto&& uref1 = x; // x is int and lvalue, so uref1's type is int&
auto&& uref2 = cx; // cx is const int and lvalue, so uref2's type is const int&
auto&& uref3 = 27; // 27 is int and rvalue, so uref3's type is int&&
我建议你阅读[文章](https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers )转发引用(以前称为通用引用)。 – TartanLlama
添加'auto && var3 = 10'作为右值引用 – bolov
因为指定的右值引用是左值。 – Pixelchemist