【发布时间】:2013-04-24 13:05:06
【问题描述】:
我正在阅读有关完美转发的文章,这是我学到的让我感到困惑的东西:
当你试图实现完美转发时,你会做这样的事情:
template<class T> // If I were to call foo with an l-value int, foo would look
void foo(T &&x); // like this: void foo(int& &&x)
然后我想,等等,这是否意味着如果我这样做了:
template<class T> // If I were to call foo with an l-value int, foo would look
void foo(T x); // like this: void foo(int& x);
但事实并非如此。 foo 看起来像这样:void foo(int x);
我的问题:为什么在完美的转发函数中,T 变成了 T& 或 T&&,但在另一个中,T 不是引用?有人能告诉我这个的确切规则吗?我需要澄清一下!
【问题讨论】:
-
当
T&&可以成为左值或右值引用时,Scott Meyers 将其称为“通用引用”。 (我觉得这个术语越来越多地被专家采用。)我建议你看看thispresentaion,并阅读他在Overload 111的文章。