简体   繁体   English

为什么push_back对左值和右值有两个重载?

[英]Why does push_back have two overloads for lvalues and rvalues?

Why would a class/function have two overloads, one for lvalue and one for rvalue? 为什么类/函数有两个重载,一个用于左值,一个用于右值?

Eg, from this video, it says we have two overloads for vector<T>::push_back 例如,从这个视频中,它说我们有两个vector<T>::push_back重载

void push_back( const T& value );
void push_back( T&& value );

Why can't we have just one overload by value, 为什么我们不能只按值重载一次,

void push_back( T value );

If it was an lvalue, value would be copied and if it was an rvalue, value would be moved. 如果它是左值,则将复制值,如果它是右值,则将移动值。 Isn't this the way how it works and guaranteed by the standard? 这不是标准的工作方式和保证吗?

使用您的按价值命题,技术上会有复制+移动或移动+移动,而其他两个重载则有一个副本或一个移动。

Besides the point others mentioned, it would also require changing the old interface. 除了其他人提到的点之外,还需要更改旧界面。 And there are times when that's simply not acceptable. 而且有时候这是不可接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM