Exception Safety Guarantees & noexcept

🔴 Advanced

📖 Definition

Exception safety guarantees describe program integrity when an exception occurs:

  • Basic Guarantee: No memory leaks occur; invariants are preserved.
  • Strong Guarantee: Operation commits fully or rolls back to the original state (copy-and-swap idiom).
  • Nothrow Guarantee (noexcept): Promises a function will never throw exceptions, critical for move constructors and destructors.

💡 Code Example

#include <iostream>

void safeFunction() noexcept {
    // Guaranteed not to throw exceptions
}

int main() {
    std::cout << "Is safeFunction noexcept? " << std::boolalpha << noexcept(safeFunction()) << "\n";
    return 0;
}