In an answer to a question about std::stack::pop()
I claimed that the reason pop
does not return the value is for exception safety reason (what happens if the copy constructor throws).
@Konrad commented that now with move semantics this is no longer relevant. Is this true?
AFAIK, move constructors can throw
, but perhaps with noexcept
it can still be achieved.
For bonus points what thread safety guarantees can this operation supply?
Move semantics allows you to avoid unnecessary copies when working with temporary objects that are about to evaporate, and whose resources can safely be taken from that temporary object and used by another.
A little bit about std::moveMove Semantics is an extremally important concept for one to understand talking about programming in c++. It is a fundamental aspect of the language that may not be that obvious and even more for one coming from another language such as Swift, C#, or Java.
A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying. For more information about move semantics, see Rvalue Reference Declarator: &&. This topic builds upon the following C++ class, MemoryBlock , which manages a memory buffer.
Of course, not every type is move-enabled and C++0x even allows throwing move constructors. As long as constructing the object from an rvalue may throw it cannot be exception-safe. However, move semantics allows you to have many types that are nothrow-constructible given an rvalue source.
Conditional support for this could be done with SFINAE. But even without such a conditional member function nothing stops you from writing:
auto stack = ...;
auto elem = std::move_if_noexcept(stack.back());
stack.pop_back();
which is makes the strong exception guarantee even in case your move constructor doesn't give the strong guarantee.
As for the bonus question, that would offer no thread-safety. Consider that, as an example, most implementations of std::vector
have three data elements (pointer to beginning of memory, pointer one beyond end of used data, pointer one beyond end of allocated memory). Move semantics allow you to move the contents of the vector without the need to reallocate and copy values, but that has nothing to do with thread safety. You would have to use thread-safe constructs to make the structure thread safe (as moving does not imply by any means atomic)
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