Boost logo

Boost :

Subject: Re: [boost] [Review:Contract] Base class preconditions not checked
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2012-09-02 13:41:46


On Sun, Sep 2, 2012 at 9:15 AM, Steven Watanabe <watanabesj_at_[hidden]> wrote:
> AMDG
>
> Test case attached. The problem is that
> the precondition test is executed when
> initializing a virtual base.

Yes, this is the case but just for constructors and is how I can check
pre before executing member intialzers.

> Virtual
> bases are always initialized by the most
> derived class, so the preconditions of
> base class constructors are never checked.

Indeed, removing the virtual from the
contract::aux::call_constructor_entry base produces:

base::pre
precondition number 2 "i >= 0" failed: file "02.cpp", line 18
terminate called after throwing an instance of 'contract::broken'
  what(): assertion "i >= 0" failed: file "02.cpp", line 18

For this code:

#include <contract.hpp>

CONTRACT_CLASS(
    class (base)
) {
    CONTRACT_CLASS_INVARIANT( void )

    CONTRACT_CONSTRUCTOR(
        public explicit (base) ( int i )
            precondition(
                std::cout << "base::pre" << std::endl,
                i >= 0
            )
    ) {
        std::cout << "base::body" << std::endl;
    }
};

CONTRACT_CLASS(
    class (deriv) extends( public base )
) {
    CONTRACT_CLASS_INVARIANT( void )

    CONTRACT_CONSTRUCTOR(
        public explicit (deriv) ( void )
        initialize( base(-1) )
    ) {
        std::cout << "derive::body" << std::endl;
    }
};

int main ( void )
{
    deriv d;
    return 0;
}

I noted this bug. I'll have to go back and see why I added the virtual
in the fist place (duplicated bases when multiple inheritance, etc)...
then I'll implement an actual fix.
https://sourceforge.net/apps/trac/contractpp/ticket/74

Thanks,
--Lorenzo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk