Many C++ programmers (myself included) would like to have destructive move semantics. Even with the current proposal we can (unsafely) say almost that:
class RAII_Handle { public: // Post-condition: h_ is valid RAII_Handle(const char* c) : h_(::open(c)) { if (h_ == nullptr) { throw false; } } RAII_Handle(RAII_Handle&& h) : h_(h.h_) { h.h_ = nullptr; } // exposition only, not self-assignment safe RAII_Handle& operator=(RAII_Handle&& h) { ::close(h_); h_ = h.h_; h.h_ = nullptr; } // if h_ is null, calling ::close( ) may crash, must protect against it ~RAII_Handle( ) { if (h_ != nullptr) { ::close(h_); } } // if h_ is null, calling ::execute( ) may crash void command(const char* c) { ::execute(h_, c); } private: RAII_Handle(const RAII_Handle&); RAII_Handle& operator=(const RAII_Handle&); Handle* h_; };
The constructor's postcondition is violated after a move. I should confess I thought this was the way it should work before reading the move proposal completely (which states that an object should be usable even after a move).
This unsafe design may be fixed by adding open( ), close( ), and is_open( ) member functions. However, if we want to use this kind of destructing move, the destructor must always protect himself from dereferencing a null pointer.
While a fully destructive move semantic would avoid this null-checking, the best solution I have read (from what other programmers propose) would allow stack-RAII'ed variables to leak resources; this is a -big no-. I fully agree with the current move proposal since its flexible enough anyway and not unsafe (unless the programmer really wants it be). For heap or placement-new'ed variables, however, the story may be different.
In the example showed above a just-moved variable's destructor will have no side effects, in fact it will just check if it must clean itself or not. This is true even for move-safe types; many classes will have to be reimplemented (std::string in mind, with at least one character as short-string optimization) so move semantics actually allow the desired performance boost. The effects of their destructors inmediatly after a move could be ignored in most cases. This document proposes to add concepts that would allow to identify these situations.
Containers that separate destruction from deallocation can use this information to avoid calls to the destructors if just deallocating will suffice. This idea was already explained by some (see references) and I was quite surprised not to see this as part of the standard. This will give us fully-destructive-like performance without actually implementing it. Implementors of containers are free to use or not this information.
// destruction: auto concept HasDestructor<typename T> see below ; auto concept HasVirtualDestructor<typename T> see below ; auto concept NothrowDestructible<typename T> see below ; concept TriviallyDestructible<typename T> see below ; concept TriviallyDestructibleAfterMove<typename T> see below ; // copy and move: auto concept MoveConstructible<typename T> see below ; auto concept CopyConstructible<typename T> see below ; concept TriviallyCopyConstructible<typename T> see below ; auto concept MoveAssignable<typename T> see below ; auto concept CopyAssignable<typename T> see below ; concept TriviallyCopyAssignable<typename T> see below ; auto concept HasSwap<typename T, typename U> see below ; auto concept Swappable<typename T> see below ; concept TriviallyReallocatable<typename T> see below ; // memory allocation: auto concept HasPlacementNew<typename T> see below ;
concept TriviallyDestructible<typename T> : NothrowDestructible<T> { }
5 Note: describes types whose destructors do not need to be executed when the object is destroyed.
6 Requires: for every type T that is a trivial type ([basic.types]), reference, or class type with a trivial
destructor ([class.dtor]), a concept map TriviallyDestructible<T> shall be implicitly defined in
namespace std.
concept TriviallyDestructibleAfterMove<typename T> : MoveConstructible<T> { }
7 Note: describes types whose destructors do not need to be executed when the object is moved inmediatly
before being destroyed.
auto concept Swappable<typename T> : HasSwap<T&, T&> { }
12 Note: describes types for which two values of that type can be swapped.
void swap(T& t, T& u); // inherited from HasSwap<T, T>
13 Postconditions: t has the value originally held by u, and u has the value originally held by t.
concept TriviallyReallocatable<typename T> { }
14 Note: describes types whose typical reallocation semantics (copying or moving the object to its new
location and then destroying the source object) are equivalent to call memcpy and not destroying
the source object.
15 Requires: for every type T that is a trivial type ([basic.types]), reference, or class type with both a
trivial copy constructor ([class.copy]) and trivial destructor ([class.dtor]), a concept map
TriviallyReallocatable<T> shall be implicitly defined in namespace std.
Special thanks to Howard Hinnant and Alisdair Meredith for their feedbacks.