Does anyone know of any C++ class which are not binary moveable?
In other words, do you know any class where this results in memory-leak or undefined behaviour?
{ // New scope
// Allocate some memory
unsigned char data1[sizeof(MyClass)];
unsigned char data2[sizeof(MyClass)];
// Create an instance of the class
::new (data1) MyClass();
// Create a pointer and run some class-specific code
MyClass* ptr = (MyClass*) data1;
ptr->MyFunction();
// Binary move everything to data2
::memcpy((void*)data2, (void*)data1, sizeof(MyClass));
::memset((void*)data1, sizeof(MyClass), 0); // for clarification
// Run some code at the new memory location, same object
ptr = (MyClass*) data2;
ptr->MyFunction();
// Run destructor at memory location2
(&*data2)->~MyClass();
} // ... out of scope. No implicit destructors called. No memory leaks.
Update:
By binary moveable I mean any object which can be binary moved to another location in memory and still function properly (the move constructor could be implemented with memcpy()).
Of course any object which has pointers pointing to them cant be moveable, that's not part of the question.
(clarify) tr1::smart_ptr, std::deque, std::vector and std::list and like classes can be binary moveable so dont bother arguing about thier internal behaviour when reallocating nodes.
(clarify) Classes handling memory allocation can be binary movable, as a binary move means you are not handling both objects after the move, and the destructor wont be called twice.