4 ms·
> I thought "contracts" were dropped out of C++20.[1] You're right, but (I'm not an authority, someone from /r/cpp would be better to speak here probably) I th
by gavinray 4y ago
> I thought "contracts" were dropped out of C++20.[1]
You're right, but (I'm not an authority, someone from /r/cpp would be better to speak here probably) I think after the Kona meeting recently they were back on the charter for next iteration
Specifically, GCC 13 has had support for Contracts in main since November:
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ea63396f6b08f88f1cde827e6cab94cd488f7fa7 https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ea63396f6b08f8...
> A big issue is, when does control enter and exit the object? What if you call a public member function from inside the object? Did you re-enter?
If you check the generated code from the end of the blogpost (see the bit that shows the Ghidra dissasembly) or the notes section, the pattern used in the plugin is:
T function() {
check_invariants(); // unless we're in constructor
result = function_body();
check_invariants(); // unless we're in a destructor
return result;
}
This does mean that a single member call can cause multiple cascading calls to check_invariants()
If "this.foo()" calls "this.bar()" which calls "this.baz()", etc.
But it's not avoidable, because in this open-world assumption we assume any member call can potentially mutate the state of the member.