问题描述
我有一个关于转发引用的具体问题。 (我认为)我理解 r 值引用和 std::move
,但我无法理解转发引用:
#include <iostream>
#include <utility>
template <typename T> class TD; // from "Effective Modern C++"
void consume(const int &i) { std::cout << "lvalue\n"; }
void consume(int &&i) { std::cout << "rvalue\n"; }
template <typename T>
void foo(T&& x) {
// TD<decltype(x)> xType; - prints int&&
consume(x);
}
int main() {
foo(1 + 2);
}
T
是 int
,没关系。如果 x
是 int&&
类型,为什么它打印“lvalue”而我们需要 std::forward
?我的意思是,这里从 int&&
到 const int&
的转换在哪里?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)