Return after a few days and again about a week later. Begin from a blank editor, without rereading the lessons first. Recall that takes effort is what makes ideas stick; rereading feels productive and mostly is not. This review is extra practice outside Day 7's time estimate.
Retrieve
Write each of these from memory, then compile it locally with warnings as errors (-Wall -Wextra -Werror, as in Day 7's build). The browser editor shows only Clang's default warnings, and never stops on one:
- Two event structs, a variant joining them and a named visitor with one overload each. Add a third alternative and confirm the build fails until the visitor handles it.
- A function template
count_matchingconstrained withstd::predicate, plus astatic_assertthrough a concept proving that a predicate over the wrong type is refused. - A move-only class holding a
std::unique_ptr, withstatic_asserts that it cannot be copied and can be moved.
Then say aloud which guarantees the compiler checked and which remain semantic contracts. The compiler checked exhaustive handling, callable signatures and deleted copies. It did not check that predicates are pure, that rules keep no references, or that a visitor's answer is correct. Those are tests and documentation.
Debug
Each fragment below compiles. Name the fault, the kind of fault, and the evidence that would catch it.
class Snapshotting final : public Rule {
public:
bool accepts(const Inventory& proposed, const Event&) const override {
last_ = &proposed; // Keeps a pointer into the staged copy.
return true;
}
mutable const Inventory* last_ = nullptr;
};A lifetime fault. proposed is the staged inventory, which is destroyed or swapped away when apply returns, so last_ dangles. Any later use is undefined behavior. A sanitizer build may catch the use; the header contract forbids the retention.
Engine first;
first.add_rule(std::make_unique<TotalLimit>(1500));
Engine second = first; // Intended: two engines with the same rules.An ownership fault, and this one does not compile: Engine's copy constructor is deleted, because two engines owning the same unique_ptr rules would destroy them twice. The fix is to build the rules twice, or to move the engine if one owner is enough. reject_copy.cpp is the test that keeps it this way.
for (std::size_t i = 0; i < events.size(); ++i) {
if (std::visit(ApplyVisitor{state_}, events[i]) != Error::none) { return {Error::invalid, i}; }
}A transaction fault: the visitor changes live state, so earlier events in a failed batch stay applied. It also discards the specific error code. The late-failure export comparison catches the first; an assertion on result.error catches the second.
Predict and run
#include <iostream>
#include <variant>
#include <string>
int main() {
std::variant<int,std::string> value = 7;
std::cout << std::holds_alternative<int>(value) << "\n";
return 0;
}Check your prediction
1Now picture this variant gaining a third alternative, double. A visitor whose overloads accept nothing a double converts to stops compiling, which is the point: it forces a decision about the new case. But an int overload is something a double converts to. A visitor with operator()(int) and operator()(const std::string&) keeps compiling and quietly truncates, so 0.5 arrives as 0: the conversion trap from Day 1. A generic catch-all also keeps compiling and silently treats doubles like the fallback. Exhaustiveness only protects you when no overload accepts the new type by conversion.
Transfer
Sketch a new transactional domain, such as a library that lends books with Lend and Return events, before writing any code. Write down: the invariant (each copy is lent to at most one reader), when rules run (after every event), the batch boundary, how a failure reports its event index, the canonical text format, and the failures you do not handle (allocation, concurrency, external effects, replay). If any of these is hard to state, the design is not ready yet.
Practice
Your turn
Write an explicit visitor Valid for std::variant<int, std::string> that returns true only for positive integers and nonempty strings. Check the four boundary cases and print:
1: valid
0: invalid
"x": valid
"": invalid#include <iostream>
#include <string>
#include <variant>
#include <vector>
using Value = std::variant<int, std::string>;
int main() {
const std::vector<Value> values{1, 0, std::string("x"), std::string()};
// Write Valid and a Show visitor, then print each value.
return 0;
}Hint 1: the idea
Make the rule for each alternative explicit, one overload each, with a shared bool return type.
Hint 2: a first step
Write a second visitor Show that prints an int as-is and a string in quotes, so the output shows which alternative each line came from.
Show one solution
#include <iostream>
#include <string>
#include <variant>
#include <vector>
using Value = std::variant<int, std::string>;
struct Valid {
bool operator()(int value) const { return value > 0; }
bool operator()(const std::string& text) const { return !text.empty(); }
};
struct Show {
std::string operator()(int value) const { return std::to_string(value); }
std::string operator()(const std::string& text) const { return "\"" + text + "\""; }
};
int main() {
const std::vector<Value> values{1, 0, std::string("x"), std::string()};
for (const Value& value : values) {
std::cout << std::visit(Show{}, value) << ": " << (std::visit(Valid{}, value) ? "valid" : "invalid") << "\n";
}
return 0;
}1: valid
0: invalid
"x": valid
"": invalidEach alternative has a boundary pair, one valid and one invalid case right at the edge, which is the smallest test that catches a wrong comparison. If any step of this review felt shaky, revisit Day 2 for ownership transfer, Day 4 for rollback and Day 5 for borrowed-view lifetimes. Concurrency and exception safety need new guarantees of their own; a deterministic single-threaded engine is a foundation for that study, not evidence that those guarantees already exist.
Completion is saved in this browser only. It does not measure mastery.