#include #include #include #include struct Log { std::array values{}; std::size_t size = 0; void add(int value) noexcept { values[size++] = value; } }; struct Member { Log& log; int id; Member(Log& target, int value) : log(target), id(value) { log.add(id); } ~Member() noexcept { log.add(-id); } }; struct Bundle { Member first; Member second; Bundle(Log& log, bool fail) : first(log, 1), second(log, 2) { if (fail) throw std::runtime_error("construction stopped"); } ~Bundle() noexcept { first.log.add(9); } }; int main() { Log failed; bool caught = false; try { Bundle b(failed, true); } catch (const std::runtime_error&) { caught = true; } assert(caught && failed.size == 4); assert((failed.values == std::array{{1, 2, -2, -1, 0, 0, 0, 0}})); Log success; { Bundle b(success, false); } assert(success.size == 5); assert((success.values == std::array{{1, 2, 9, -2, -1, 0, 0, 0}})); }