January 2018
Intermediate to advanced
374 pages
9h 53m
English
Take a look at the following example. If the branches_ = ot.branches_ operation throws an exception due to being out of memory (branches_ might be a very big member variable), the tree0 method will be left in an invalid state containing a copy of leafs_ from tree1 and branches_ that it had before:
struct Leaf { /* ... */ };struct Branch { /* ... */ };class OakTree {public: auto& operator=(const OakTree& other) { leafs_ = other.leafs_; // If copying the branches throws, only the leafs has been // copied and the OakTree is left in an invalid state branches_ = other.branches_; *this; } std::vector<Leaf> leafs_; std::vector<Branch> branches_;};
auto save_to_disk(const std::vector<OakTree>& trees) { // Persist all ...