| ▲ | Joker_vD a day ago | |
First of all, there is a question of syntax, which ironically could be solved by taking a page out of BCPL's book (the irony is that BCPL is C's ancestor, but C lost several of its more advanced features, like statement expressions):
Second, there is a question of semantics. You only get to have 1 destructor, and it's called unconditionally — so we force the author of Foo (or of any class, really) to either 1) write the destructor that can properly destruct an unitialized object which is a bit tricky since reading any of the fields of such an object is UB; 2) shift the blame to the users: "don't do that with this class" — which completely negates your "decided at the call site (not in the class declaration)" point, because this decision is made in the class declaration, and can be made only there, not at the call site.So as usual with C++, we end up with a half-working system, with subtle footguns, pervading all of the program code, and impossible to ignore. | ||