简体   繁体   English

C ++ 11移动语义和STL容器

[英]C++ 11 Move Semantics and STL Containers

Regarding move semantics and containers: 关于移动语义和容器:

  1. I know STL containers take advantage of move when the move operations are defined in the elements type. 我知道当元素类型中定义了移动操作时,STL容器会利用移动。 But how does it know if an element has defined move operations or not? 但是它如何知道元素是否定义了移动操作?

  2. Why don't STL containers just invoke std::move() on the elements anyway, regardless of whether the element has defined the move operations or not? 为什么STL容器无论如何都不会在元素上调用std :: move(),无论元素是否定义了移动操作? I'm asking this because I know you can invoke std::move() on objects even if its type does not define any move operations. 我问这个是因为我知道你可以在对象上调用std :: move(),即使它的类型没有定义任何移动操作。

Thank you. 谢谢。

Long story short, that's exactly what they do, calling std::move without caring if it will be able to move or just copy. 长话短说,这正是他们所做的,如果它能够移动或只是复制,调用std::move而不关心。

It's worth noting that some functions offering the strong exception guarantee, such as std::vector::resize , will call the lesser known std::move_if_nothrow instead of std::move . 值得注意的是,一些提供强异常保证的函数,比如std::vector::resize ,会调用鲜为人知的std::move_if_nothrow而不是std::move

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

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