Regarding move semantics and containers:
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?
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With