【发布时间】:2019-03-17 22:19:02
【问题描述】:
假设我们有一些代码
template <typename T>
void Foo(T&& param);
int x = 5;
// deduced Foo<int&>(int&).
// Argument type A = int. T = int&.
Foo(x);
int& rx = x;
// deduced Foo<int&>(int& &&) -> Foo<int&>(int&).
// But... argument type A = int&, an lvalue reference to int.
// Is T = int& or under the hood it is T = int& &?
Foo(rx);
根据https://en.cppreference.com/w/cpp/language/template_argument_deduction
4) 如果 P 是对 cv 不合格模板参数的右值引用(所谓的转发引用),并且对应的函数调用参数是左值,则使用对 A 的类型左值引用代替 A 进行推导
我想知道的是:'引用折叠'规则是否应用于推导类型 T(而不是 P,参数类型)?
那么我们真的有 for 'Foo(rx)' T = int& &,它折叠成 T = int& 吗?
【问题讨论】:
标签: c++ c++11 reference forwarding-reference