其他分享
首页 > 其他分享> > c – 为什么\u0026\u0026有时绑定左值而不是其他时间?

c – 为什么\u0026\u0026有时绑定左值而不是其他时间?

作者:互联网

好的,我有这个代码:

struct Mine{
    template<typename T>
    Mine(T&& x){
    }
};
void nFoo(int&& x){
}
void sFoo(Mine x){
}

nFoo采用int&&直接,而sFoo做了一些finagling以获得相同的效果.

考虑以下代码:

nFoo(12); //Works

int x = 0;
nFoo(x);  //Cannot bind int (lvalue) to int&&

sFoo(12); //Works
sFoo(x);  //Works

为什么int&&有时允许从左值绑定,有时不允许?

解决方法:

Why does the int&& sometimes allow binding from lvalues and sometimes not?

INT&安培;&安培;不会绑定到左值,因为它是右值引用.

在T是模板参数的上下文中,T&&和lvalue绑定,因为它是forwarding reference.它不是右值引用,即使语法几乎相同.

标签:rvalue,c,rvalue-reference,lvalue
来源: https://codeday.me/bug/20190722/1502719.html