为什么STD :: MOVE将RVALUE参考作为参数?
根据 cppreference.com ,,
move
具有签名
template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept;
为什么要进行rvalue参考t&amp;&amp; t
作为灌溉?
另外,当我尝试以下代码时,
void foo(int&& bar) {
cout << "baz" << endl;
}
int main(){
int a;
foo(a);
}
我从编译器“ RVALUE参考不能绑定到lvalue”中收到错误
?我很困惑。
According to cppreference.com, move
has signature
template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept;
Why does it take a rvalue reference T&& t
as its arugment?
Also when I tried the following code
void foo(int&& bar) {
cout << "baz" << endl;
}
int main(){
int a;
foo(a);
}
I got an error from the compiler "an rvalue reference cannot be bound to an lvalue"
What is going on? I'm so confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是RVALUE参考,而是A 转发参考;可以保留论点的价值类别。这意味着
std ::移动
可以同时使用lvalue和rvalue,并无条件地将它们转换为rvalue。另一方面,
int&amp;&amp;
是RVALUE参考;请注意此处的区别,如果函数模板参数具有类型t&amp;&amp;
带有模板参数t
,则是推论类型t
,参数是转发参考。It's not an rvalue reference, but a forwarding reference; which could preserve the value category of the argument. That means
std::move
could take both lvalue and rvalue, and convert them to rvalue unconditionally.On the other hand,
int&&
is an rvalue reference; note the difference here, if a function template parameter has typeT&&
with template parameterT
, i.e. a deduced typeT
, the parameter is a forwarding reference.