I have a C++03 library with lots of classes. I'm interested in trying to take advantage of Rvalues and move semantics under C++11 for two or three critical classes. Once of the critical classes is a SecBlock
, which is effectively a secure array class. It has a secure allocator and deallocator, and its used nearly everywhere.
SecBlock
looks like so, where T
might be a byte
or an int
:
template <class T, class A = AllocatorWithCleanup<T> >
class SecBlock
{
...
private:
A m_alloc;
size_type m_size;
T *m_ptr;
}
If I am reading Thomas Becker's "C++ Rvalue References" treatise correctly, I can use a simple swap
because SecBlock
does not have a base class (no need for Base(std::move(rhs))
:
template <class T, class A = AllocatorWithCleanup<T> >
class SecBlock
{
...
#if CXX11_AVAILABLE
SecBlock(SecBlock&& t)
{
std::swap(*this, t);
}
SecBlock& operator=(SecBlock&& t)
{
if(this != &t)
std::swap(*this, t);
return *this;
}
#endif
private:
A m_alloc;
size_type m_size;
T *m_ptr;
}
We guard on CXX11_AVAILABLE
because the library is a C++03 library at heart.
It's probably obvious I have some open questions. For instance, do I have to guard for self assignment in operator=
? (I suspect not, but I think its best to show the code and take the comments).
I'm also interested in knowing if a derived class must implement the semantics also, even if they add no members.
Is the code above correct, or are there gaps that need to be addressed? If there are gaps, what are they?