简体   繁体   English

为什么vector :: push_back有两个重载?

[英]Why are there two overloads for vector::push_back?

Why doesn't vector::push_back take a forwarding reference instead of having two overloads? 为什么vector::push_back采用转发引用而不是有两个重载? I've read that the only reason you'd want to overload on lvalues and rvalues is if your functions do something differently for them, so how do both overloads of vector::push_back differ other than moving/copying? 我读过您想在lvalues和rvalues上进行重载的唯一原因是,如果您的函数对它们执行的操作有所不同,那么除了移动/复制之外, vector::push_back两个重载又有何不同?

I did this largely just because of how the situation evolved. 我这样做主要是因为情况如何演变。 Prior to C++11, there was only: 在C ++ 11之前,只有:

vector<T>::push_back(const T&);

With the introduction of rvalue references I recommended the addition of the overload: 通过引入右值引用,我建议添加重载:

vector<T>::push_back(T&&);

instead of changing the original signature to: 而不是将原始签名更改为:

template <class U> vector<T>::push_back(U&&);

Part of this decision was made because of some concerns about backward compatibility (whether warranted or not), and to ease concerns of both vendors and others on the committee that this was a simple addition of functionality, and not a change to existing functionality. 之所以做出此决定,部分原因是出于对向后兼容性的担忧(无论是否保证),并且缓解了供应商和委员会其他成员的担忧,即这只是对功能的简单添加 ,而不是对现有功能的更改。

If I were redesigning vector from scratch today, I would seriously consider just having: 如果今天我要从头开始重新设计vector ,那么我会认真考虑一下:

template <class U> vector<T>::push_back(U&&);

or perhaps just: 也许只是:

template <class ...Args> vector<T>::emplace_back(Args&& ...);

More details than you probably want to know about are in N1858 . N1858中提供了比您可能想知道更多的详细信息。

Why not push_back by value? 为什么不按值push_back

This question has been marked as a duplicate of: 该问题被标记为重复项:

Why do C++11 std containers have pass-by-ref and pass-by-rvalue insert/push methods? 为什么C ++ 11 std容器具有pass-by-ref和pass-by-rvalue插入/推送方法?

which asks this question. 问这个问题。 So I figured it would be the polite thing to do to actually address that aspect in this answer... 所以我认为在这个答案中真正解决这一方面是有礼貌的事情...

For lvalues and xvalues, a push_back(T) would cost an extra move construction compared to the by-reference solutions. 对于左值和左值,与按引用解决方案相比, push_back(T)将花费额外的移动构造。 xvalues would require 2 move constructions and lvalues would require 1 copy construction and 1 move construction. xvalues需要2个移动构造,而lvalues需要1个复制构造和1个move构造。

In contrast, with the current design, lvalues cost 1 copy construction and xvalues cost 1 move construction. 相反,在当前设计中,左值花费1个副本构造,而左值花费1个移动构造。

For some types T , move construction is not cheap. 对于某些类型T ,移动结构并不便宜。 It would be a poor design choice for vector<T> to assume that T is always cheaply movable. 假设T总是便宜地移动,对于vector<T> ,这是一个糟糕的设计选择。 For example what if T is std::array<double, 100> ? 例如,如果Tstd::array<double, 100>怎么办? Changing to the by-value design would require 2 copy constructions instead of 1 to push_back (except for prvalues). 更改为按值设计将需要2个副本构造而不是push_back 1个构造(prvalues除外)。

The by-value solution does have advantages, and times when it should be used. 按价值解决方案确实具有优势,并且应该使用它的时间。 It is just that vector<T>::push_back() is not one of those times. 只是vector<T>::push_back()不在那些时候之一。

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

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