C++
Beginner
1 min read
Exception Safety Guarantees and noexcept
Example
#include <iostream>
#include <utility> // std::swap
#include <string>
#include <vector>
class SafeBuffer {
public:
explicit SafeBuffer(std::size_t n)
: data_(new int[n]), size_(n) {}
// Destructor — no-throw guarantee
~SafeBuffer() noexcept { delete[] data_; }
// Copy constructor — basic guarantee (may throw on alloc)
SafeBuffer(const SafeBuffer& other)
: data_(new int[other.size_]), size_(other.size_) {
std::copy(other.data_, other.data_ + size_, data_);
}
// Copy-and-swap idiom — strong guarantee on assignment
SafeBuffer& operator=(SafeBuffer other) noexcept {
swap(*this, other); // no-throw swap
return *this;
}
// Move constructor — no-throw guarantee
SafeBuffer(SafeBuffer&& other) noexcept
: data_(other.data_), size_(other.size_) {
other.data_ = nullptr;
other.size_ = 0;
}
friend void swap(SafeBuffer& a, SafeBuffer& b) noexcept {
std::swap(a.data_, b.data_);
std::swap(a.size_, b.size_);
}
std::size_t size() const noexcept { return size_; }
private:
int* data_;
std::size_t size_;
};
// noexcept propagation in template
template<typename T>
void conditionalSwap(T& a, T& b)
noexcept(noexcept(std::swap(a, b))) {
std::swap(a, b);
}
int main() {
SafeBuffer a(4), b(8);
a = b; // strong guarantee via copy-and-swap
std::cout << "a.size() after assignment = " << a.size() << "\n";
int x = 1, y = 2;
conditionalSwap(x, y);
std::cout << "x=" << x << " y=" << y << "\n";
return 0;
}