Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

C++ exception safety: basic, strong, and no-throw guarantees

The basic guarantee permits partial changes that leave valid state. The strong guarantee leaves no observable change on exception. The no-throw guarantee prevents exceptions from reaching the caller.

Version: ISO C++23; implementation support may vary

Syntax

void update_basic(State& live, bool fail);
void update_strong(State& live, bool fail);
void commit(State& live, State& staged) noexcept;

// Copy-and-swap inside update_strong:
State staged = live;
update_basic(staged, fail);
commit(live, staged);

// Inside commit, check the selected swap expression without evaluating it:
static_assert(noexcept(std::swap(live, staged)));

Basic guarantee. If an exception leaves the operation, no resources leak and the invariants of the objects covered by the contract still hold. Valid does not mean unchanged: the first of two updates can remain visible if the second throws.

Strong guarantee. If an exception leaves the operation, the state covered by its contract is observably the same as before the call. Success can still change state. With copy-and-swap, make an independent copy and perform the potentially throwing changes on that copy. If preparation throws, the original is untouched. Only after preparation succeeds do you commit with a swap that must succeed without throwing. The temporary takes ownership of the old state and releases it when destroyed, without throwing.

No-throw guarantee. When the preconditions hold, no exception reaches the caller. The operation may catch exceptions internally or report failure another way, such as with an error code. The noexcept specifier does not validate the implementation or recover state. If an exception attempts to escape a noexcept function, std::terminate is invoked. The noexcept(expr) operator tests whether the expression is potentially throwing without evaluating it.

Check the assumptions. std::swap is not unconditionally non-throwing: check the selected overload and its preconditions. A shallow copy that shares mutable state, or an irreversible external side effect during preparation, can break the strong guarantee.

Examples

#include <iostream>
#include <utility>

struct State {
    int first;
    int second;
};

struct InjectedFailure {};

void update_basic(State& live, bool fail) {
    live.first = 10;
    if (fail) {
        throw InjectedFailure{};
    }
    live.second = 20;
}

void commit(State& live, State& staged) noexcept {
    static_assert(noexcept(std::swap(live, staged)));
    std::swap(live, staged);
}

void update_strong(State& live, bool fail) {
    State staged = live;
    update_basic(staged, fail);
    commit(live, staged);
}

void print(const char* label, const State& state) {
    std::cout << label << ": " << state.first << ' ' << state.second << '\n';
}

int main() {
    State basic{1, 2};
    State strong{1, 2};

    try {
        update_basic(basic, true);
    } catch (const InjectedFailure&) {
        print("basic failure", basic);
    }

    try {
        update_strong(strong, true);
    } catch (const InjectedFailure&) {
        print("strong failure", strong);
    }

    update_strong(strong, false);
    print("strong success", strong);
}

Result

basic failure: 10 2
strong failure: 1 2
strong success: 10 20

Both objects start at `1 2`. The injected exception after the first write leaves `basic` in the valid state `10 2`. In `update_strong`, only `staged` changes, so `strong` retains `1 2`. With no injected exception, `commit` publishes `10 20`.

Related lessons

  • C++ · Lesson 15 · Advanced · Version: 3

Sources