根据 N3485 §23.3.2.2:
(...) the implicit move constructor and move assignment operator for array require that T be MoveConstructible or MoveAssignable, respectively.
所以,std::array
支持 move 语义,如果它的元素类型支持的话。太好了!
但是,这究竟意味着什么?我倾向于将这种类型描绘为提供符合 STL 接口(interface)的数组的更安全版本,但如果这是真的,那么 std::array
如何 move 构造其元素?我可以对普通数组做同样的事情吗?
最佳答案
However, what does this really mean?
这意味着,如果元素类型是可 move 的,那么数组类型也是。
std::array<movable, 42> move_from = {...};
std::array<movable, 42> move_to = std::move(move_from); // moves all the elements
I tend to picture this type as a safer version of an array providing an STL-compliant interface
不是真的。它是一个数组的包装器,赋予它与聚合类相同的语义 - 包括复制和 move 它的能力。
how can an
std::array
move-construct its elements?
与任何其他聚合的方式完全相同。它的隐式 move 构造函数将 move 构造它的所有成员,包括任何成员数组的元素。
Can I do the same with an ordinary array?
仅当您将其包装在类类型中时,如 std::array
所做的那样。
关于c++ - move 构造函数和 `std::array`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22613991/