Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract inheritance does not work as expected #18

Open
valmat opened this Issue Mar 10, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@valmat
Copy link

valmat commented Mar 10, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.