In C++, what do the basic, strong and nothrow exception-safety guarantees each promise to the caller of a function?
answer
- three promises, increasing strength
- invariants hold, state may change
- commit or roll back
- cannot fail at all
basics
~20 sBasic: if the operation throws, nothing leaks and every object still meets its invariants, but its state may have changed. Strong: if it throws, the state is exactly as before. Nothrow: it never throws, so callers can build the other two on it.
solid answer
~50 sThe three guarantees describe what a caller can rely on *after* an operation exits via an exception. **Basic**: no resource leaks and every object is still valid, usable and destructible, though its value may be any valid one. **Strong**: commit-or-rollback; on failure the observable state is what it was before the call. **Nothrow**: the operation never exits via an exception, which C++ lets you state with `noexcept`. The standard library spells them out per function: a single-element `insert` into a container has "no effects" if it throws, `push_back` likewise when the element is copyable, while inserting into the middle of a `std::vector` gives only the basic guarantee if the element's copy throws, and `erase`, `clear`, `pop_back` and `swap` on standard containers do not throw. The weakest guarantee a function's callers need is the one it should document.
go deeper
Be able to name the three guarantees and say in one sentence what each promises after an exception.
Tie each guarantee to concrete standard library behaviour: push_back has no effects on failure, insertion in the middle of a vector is only basic, swap and erase do not throw.
Show you can read a function and say which guarantee it gives, find the step that breaks an invariant, and fix it with a non-throwing commit or undo.
Argue where strong is worth its cost and where basic is enough, and make the documented guarantee part of each API's contract.
## Why the guarantees exist When an operation exits via an exception, the caller has to know what state it is now holding. "It threw" is not enough: is the inventory half-updated? Did an item leak? Can the object still be used, or only destroyed? The three **exception-safety guarantees** answer that question with a fixed vocabulary, and C++ code reviews and library documentation use it constantly. ## The three levels | Guarantee | Promise if the operation throws | Typical example | |---|---|---| | **Basic** | no leaks; all objects still satisfy their invariants and can be used or destroyed; values may have changed | inserting into the middle of a `std::vector` whose element copy throws | | **Strong** | commit-or-rollback: the observable state is exactly as before the call | `std::vector::push_back` of a copyable element; single-element `insert` into `std::map` | | **Nothrow** | the operation never throws | destructors, `swap` on standard containers, `erase` by iterator, `clear`, deallocation | Below the basic guarantee is **no guarantee**: a function that can leave an object with a broken invariant, such as an inventory whose item list and index disagree. In C++ that is simply a bug, because every later operation on the object is built on a false assumption. ## What the C++ standard actually says The standard does not use the words "basic" and "strong" in its normative text; it states the effect per function, and the vocabulary maps onto it: - **"No effects"** is the strong guarantee. The container requirements say that if an exception is thrown by an `insert()` or `emplace()` inserting a single element, or by `push_back()`, `push_front()`, `emplace_back()` or `emplace_front()`, that function has no effects, unless a specific container says otherwise. - **`std::vector` says otherwise.** Its modifiers have no effects when the exception does not come from the element's copy or move operations; inserting a single element *at the end* has no effects when the element is copy-insertable or has a non-throwing move constructor. Inserting elsewhere, when the element's copy or assignment throws, has no such promise: the standard says nothing about the contents, so callers can rely only on the vector remaining a valid object, which is the basic guarantee. - **"Does not throw"** is the nothrow guarantee: no `erase()`, `clear()`, `pop_back()` or `pop_front()` throws, and no standard container `swap()` throws, with exceptions only for user-supplied comparators. Every destructor in the standard library behaves as if it were `noexcept`. ## The inventory container as an example Take an inventory class holding a `std::vector<Item>` and a `std::map<std::string, std::size_t>` index from SKU to position: 1. **No guarantee**: `insert` pushes to the vector, then emplaces into the map. If the map throws `std::bad_alloc`, the vector holds an item the index does not know about. 2. **Basic**: a bulk `insert_all` that stops on the first bad item, keeping the items already added consistently indexed. The caller knows the inventory is valid but not which items made it in. 3. **Strong**: single-item `insert` that either adds the item to both structures or leaves both untouched. 4. **Nothrow**: `size()`, `swap()`, and the destructor. ## Choosing which guarantee to offer - The **basic** guarantee is the floor for every function in a C++ codebase; anything less is a defect. - The **strong** guarantee is worth offering where it is cheap or where callers need atomicity, typically a single insertion or a commit step. Offering it for a bulk operation often means copying everything first, which may cost more than callers want to pay. - The **nothrow** guarantee is what makes the other two possible: rollback code and destructors must not themselves fail. ## Stating the guarantee in an API A guarantee only helps callers if it is written down where they look: - Put it in the function's comment in one word, "strong" or "basic", plus the exceptions it can throw. - For a class, state the guarantee of its mutators once and list the exceptions. - When a function's guarantee depends on its template arguments, say so, as the standard does for `std::vector::push_back`: no effects on failure only when the element is copyable or has a non-throwing move constructor. ## Composition Guarantees compose upward only through nothrow steps. A function that performs two strong operations in a row is *not* automatically strong, because the second can fail after the first has committed; to make it strong you need to undo the first with an operation that cannot throw, or restructure the work so that only the last step commits.
- Why does inserting into the middle of a std::vector give only the basic guarantee when the element's copy throws?Making room shifts existing elements by copy or move assignment. If one of those throws halfway, some elements have already been overwritten and putting them back would need more copies that could also fail. The standard therefore promises no effects only when the exception comes from somewhere else, or for single-element insertion at the end; otherwise it says nothing about the contents, and callers can rely only on a valid vector.
- Is calling two strong-guarantee operations in sequence strong?No. If the second throws, the first has already committed, so the combined state is neither before nor after. To get the strong guarantee for the pair you need a non-throwing undo for the first step, or you must do all throwing work off to the side and make the visible change with non-throwing operations only.
Think of a bank transfer screen. Basic: if it fails, both accounts still exist and balance, though money may have moved. Strong: if it fails, both balances are what they were. Nothrow: the step simply cannot fail, like reading a balance you already have on screen.
- Basic exception safety means the object is left exactly as before.
- Two strong-guarantee calls in a row give a strong-guarantee function.
- Every standard library function offers the strong guarantee.
- std::vector::insert in the middle always has no effects if it throws.
- The nothrow guarantee only means the function rarely throws.