标准库对自我移动的分配有什么保证? [英] What does the standard library guarantee about self move assignment?
问题描述
C ++ 11标准关于标准库的自我移动赋值是什么?更具体地说,如何保证什么是 selfAssign
?
What does the C++11 standard say about self move assignment in relation to the standard library? To be more concrete, what, if anything, is guaranteed about what selfAssign
does?
template<class T>
std::vector<T> selfAssign(std::vector<T> v) {
v = std::move(v);
return v;
}
推荐答案
17.6.4.9函数参数[ res.on.arguments]
17.6.4.9 Function arguments [res.on.arguments]
1以下每个适用于C ++标准库中定义的函数
的所有参数,除非另有明确说明。
1 Each of the following applies to all arguments to functions defined in the C++ standard library, unless explicitly stated otherwise.
...
- 到右值引用参数,实现可以假定该参数是对
这个参数的唯一引用。 [注意:如果参数是
形式的通用参数T&&并且类型A的左值被绑定,该参数绑定到
左值引用(14.8.2.1),因此不被先前的
句子覆盖。 - end note] [注意:如果程序将一个左值传递给
xvalue,同时将该值传递给库函数(例如通过
调用参数move(x)的函数),程序
有效地要求该函数将该左值作为临时对象。
实现可以自由地优化掉别名检查,如果参数是anlvalue,那么可能需要
。 -endnote]
所以,执行 std :: vector< T,A> :: operator =(vector&&&&&& other)
允许假设 other
是prvalue。如果其他
是prvalue,则不可能进行自我移动分配。
So, the implementation of std::vector<T, A>::operator=(vector&& other)
is allowed to assume that other
is a prvalue. And if other
is a prvalue, self-move-assignment is not possible.
v
将处于无资源状态(0容量)。如果 v
已经有0个容量,那么这将是一个无操作。
v
will be left in a resource-less state (0 capacity). If v
already has 0 capacity, then this will be a no-op.
这篇关于标准库对自我移动的分配有什么保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!