LVALUE参考可以与RVALUE参考结合,但不能与STD :: MOVE的结果结合。
我的功能具有lvalue参考作为参数,我发现我也可以通过 对此的rvalue引用。但是,当我将std :: Move
的结果传递给功能时,我会收到一个错误,告诉我我不能将非const lvalue引用绑定到rvalue。
#include <iostream>
#include <string>
template<typename T>
void foo(T& a) { }
struct A {
std::string s;
};
int main(int argc,char** argv) {
A a{"a"};
A&& ra = std::move(a);
foo(ra); // OK
foo(std::move(a)); // Error
return 0;
}
我当时认为std ::移动
还返回rvalue参考。 使用中间RVALUE参考作为参数和直接使用std :: Move
作为参数的结果有什么区别?
I have a function that has a lvalue reference as argument, and I found that I can also pass
a rvalue reference to it. However, when I passing the result of std::move
to the function, I get an error which tells me that I cannot bind non-const lvalue reference to an rvalue.
#include <iostream>
#include <string>
template<typename T>
void foo(T& a) { }
struct A {
std::string s;
};
int main(int argc,char** argv) {
A a{"a"};
A&& ra = std::move(a);
foo(ra); // OK
foo(std::move(a)); // Error
return 0;
}
I was thinking that std::move
also returns a rvalue reference.
What is the difference between using an intermediate rvalue reference as argument and directly using the result of std::move
as argument?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RA
是一个命名变量。因此,它是一个LVALUE,可以与LVALUE参考结合。std ::移动(a)
返回a&amp;&amp;
,但是返回的值是一个prvalue。 LVALUE参考不能与Prvalues结合。ra
is a named variable. As such, it is an lvalue and can be bound to an lvalue reference.std::move(a)
returns aA&&
, but that returned value is a prvalue. lvalue references cannot bind to prvalues.