The Copy-and-Swap idiom provides the Strong Exception Guarantee (either the operation succeeds completely, or the object remains unmodified in its original state):
class SmartBuffer {
size_t m_size;
int* m_data;
public:
friend void swap(SmartBuffer& first, SmartBuffer& second) noexcept {
std::swap(first.m_size, second.m_size);
std::swap(first.m_data, second.m_data);
}
SmartBuffer& operator=(SmartBuffer other) noexcept { // Pass by value (creates copy)
swap(*this, other); // Swap with temporary copy
return *this;
} // Old data is safely destroyed when temporary 'other' leaves scope!
};