inhwank
inhwank

Reputation: 85

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. 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? I'm asking this because I know you can invoke std::move() on objects even if its type does not define any move operations.

Thank you.

Upvotes: 4

Views: 1321

Answers (1)

Tiago Gomes
Tiago Gomes

Reputation: 386

Long story short, that's exactly what they do, calling std::move without caring if it will be able to move or just copy.

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.

Upvotes: 8

Related Questions