【发布时间】:2016-10-19 21:48:40
【问题描述】:
好的,我有这个代码:
struct Mine{
template<typename T>
Mine(T&& x){
}
};
void nFoo(int&& x){
}
void sFoo(Mine x){
}
nFoo 直接采用int&&,而sFoo 做了一些调整以获得相同的效果。
考虑这段代码:
nFoo(12); //Works
int x = 0;
nFoo(x); //Cannot bind int (lvalue) to int&&
sFoo(12); //Works
sFoo(x); //Works
为什么int&& 有时允许来自lvalues 的绑定,有时不允许?
【问题讨论】:
-
Scott Meyers 在Effective Modern C++ 中专门有一篇很好的文章。
标签: c++ rvalue-reference lvalue rvalue