Contract inheritance does not work as expected
P0542R5 says: If an overriding function specifies contract conditions, it shall specify the same list of contract conditions as its overridden functions; no diagnostic is required if corresponding conditions will always evaluate to the same value. Otherwise, it is considered to have the list of contract conditions from one of its overridden functions
So in folowing example i expect contract violation
struct Base
{
virtual int foo(int n)
[[expects: n < 10]]
[[ensures r: r > 100]]
{
return n*n;
}
};
struct Derived : Base
{
virtual int foo(int n) override // inherits contracts from Base
{
return n*2;
}
};
But no contract violations are happening
...
int main()
{
Derived obj;
obj1.foo(100); // <-- expected contract violation
Derived* pobj1 = new Derived();
pobj1->foo(100); // <-- expected contract violation
Base* pobj2 = new Derived();
pobj2->foo(100); // <-- expected contract violation
delete pobj1;
delete pobj2;
return 0;
}
Note if repeat the contracts from Base to Derived, everything works as expected.
As stated in README.md, this is a known issue (unimplemented as of 3/28/2019). Expect this feature before June 2019.
Anyone got contract inheritance working?