|
|
|
|
|
by alphaalpha101
3220 days ago
|
|
I don't really agree that it's inelegant to decide whether the reason for quitting is a success or an exception, or whether it's necessary. Like, you've either committed, in which case you shouldn't need to do any cleanup, or you haven't committed, in which case you need to clean up, right? If you have an object representing some sort of transaction, and you just have actually-do-all-the-work-at-once-on-commit-being-called semantics, you don't actually need to do anything in your destructor at all, right? template<typename T>
class transaction {
std::vector<T> things_to_do;
public:
void add_work_item(T t) {
things_to_do.push_back(t);
}
void commit() {
for (auto t : things_to_do) {
t.do();
}
}
};
|
|
What we want actually executed in the end (in terms of control flow - not necessarily what we would be willing to code), is something like the following clean procedural code.