Rvalue copy and and assignment for secure array class 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 &lt;class T, class A = AllocatorWithCleanup&lt;T&gt; &gt;
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 &lt;class T, class A = AllocatorWithCleanup&lt;T&gt; &gt;
class SecBlock
{
   ...

#if CXX11_AVAILABLE
    SecBlock(SecBlock&amp;&amp; t)
    {
        std::swap(*this, t);
    }
    SecBlock&amp; operator=(SecBlock&amp;&amp; t)
    {
        if(this != &amp;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?
This code does not function.

Let's take a look at your move constructor:    

SecBlock(SecBlock&amp;&amp; t)
{
    std::swap(*this, t);
}


This caused a stack overflow on my system. Why? Imagine if the swap function was implemented like this:

template &lt;typename T&gt;
void swap (T &amp;t1, T &amp;t2)
{
    T temp = std::move (t1) ;
    t1 = std::move (t2) ;
    t2 = std::move (temp) ;
}


In fact, this is how it is implemented by Visual Studio 2012. The first line T temp = std::move (t1) ;, calls your move constructor. This causes you to recursively call your move constructor until your program inevitably crashes.

Another problem is that before the swap, m_size and m_ptr for *this will be uninitialized. So even if you fixed your move constructor, you will be transferring uninitialized data to t. Assuming you're destructor does some clean-up, this could lead to undefined behavior.

All in all, both member-functions are a mess.
Check out the rule of 5 for more help.