Reputation: 943
I generally (try to) write exception safe copy assignment operators using the copy-swap idiom, and I was wondering if I should be concerned about exceptions when writing the move assignement operators. Here is an example of a copy assignement operator:
template<class T>
CLArray<T>&
CLArray<T>::operator=( const CLArray& rhs )
{
CLArray tmp( rhs );
std::swap( size_, tmp.size_ );
std::swap( data_, tmp.data_ );
return *this;
}
But what about the move assignement ? I mean, if an exception is thrown somewhere else in the code during THIS move operation, I will lose the state of both objects right ? So I would have to create a local copy first and then delete everything but the newly created CLArray
...
template <class T>
CLArray<T>&
CLArray<T>::operator=( CLArray<T>&& rhs )
{
size_ = rhs.size_;
data_ = std::move( rhs.data_ );
return *this;
}
Please note that data_
is a std::vector, and thanks for the answers !
Upvotes: 3
Views: 727
Reputation: 4760
Anyway you should add a swap
member function and leverage the (copy / move) constructors in the (copy / move) assignment operators. (And put the operations that can't throw after those that might.)
Example (here inline in the class for brevity):
template<typename T>
class CLArray {
public:
void swap( CLArray& other )
{
std::swap( data_, other.data_ );
std::swap( size_, other.size_ );
}
CLArray( const CLArray& other )
: data_( other.data_ ), size_( other.size_ )
{
}
CLArray& operator=( const CLArray& rhs )
{
CLArray( rhs ).swap( *this );
return *this;
}
CLArray( CLArray&& other )
: data_( std::move( other.data_ ) )
{
size_ = other.size_;
other.size_ = 0;
}
CLArray& operator=( CLArray&& rhs )
{
CLArray( std::move( rhs ) ).swap( *this );
return *this;
}
// ...
private:
std::vector<T> data_;
std::size_t size_;
};
See C9 Lectures: Stephan T. Lavavej - Standard Template Library (STL), 9 of n (rvalue references) (video and STL's remarks and code in the comments).
You may also want to read Dave Abrahams's articles Your Next Assignment… and Exceptionally Moving!.
Upvotes: 3
Reputation: 254471
Indeed, it can be difficult or impossible to provide exception guarantees if a move constructor might throw.
I would suggest doing as the standard library does: document that certain operations only have exception guarantees (or, in some cases, are only permitted) if move-construction of T
doesn't throw. Ensuring the guarantee by copying the object destroys the benefit of move-assignment for all types, not just the (very rare) ones that might throw.
Upvotes: 5