Item-29 Strive for exception-safe code
· β 8 min read
Exception-safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thrown. Such functions offer the basic, strong, and nothrow guarantees.
Exception-safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thrown. Such functions offer the basic, strong, and nothrow guarantees.
Avoid returning handles to object internals to increase encapsulation, help const
member functions act const
, and minimize the creation of dangling handles.
Avoid casts and develop a cast-free alternative whenever practical, especially dynamic_cast
in performance-sensitive code.
Postponing variable definitions as long as possible increases program clarity and improves progranm efficiency.
When std::swap
would be inefficient for your type,provide a non-throwing swap
member function, a non-member swap
calling the member, and possibly a specialized std::swap
for the case of classes (not templates).