Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 11 Move Semantics and STL Containers

Tags:

c++

c++11

stl

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.

like image 520
inhwank Avatar asked Feb 13 '23 21:02

inhwank


1 Answers

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.

like image 57
Tiago Gomes Avatar answered Feb 16 '23 09:02

Tiago Gomes