Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: 2nd CS split from 1st

...

Code Block
bgColor#ccccff
langcpp
#include <stdexcept>
#include <type_traits>

struct S : std::runtime_error {
  S(const char *msg) : std::runtime_error(msg) {}
};
 
static_assert(std::is_nothrow_copy_constructible<S>::value,
              "S must be nothrow copy constructible");

void g() {
  // If some condition doesn't hold...
  throw S("Condition did not hold");
}

void f() {
  try {
    g();
  } catch (S &s) {
    // Handle error
  }
}

Compliant Solution

If the exception type cannot be modified to inherit from std::runtime_error, a data member of that type is an unexpected but a legitimate implementation strategy, as shown in this compliant solution:

...