Boost logo

Boost :

Subject: [boost] [contract] nothing special about nested classes
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2012-03-29 09:57:52


Hello all,

In the _many_ Contract Programming references that I have read (including
N1962):
http://contractpp.svn.sourceforge.net/viewvc/contractpp/trunk/doc/html/contract__/bibliography.html

I didn't find anything special about contracts for nested classes. I think
that is because nested classes are executed at run-time "orthogonally" with
respect to their enclosing classes. In other words, nesting a class only
nests it's name (and changes some visibility rules) but it does not effect
the run-time execution of the class and therefore it does not affect the
contracts checked for the nested class.

For example, the execution of a nested class y should only check y's
invariants and not also the invariants of its enclosing class x:

#include <contract.hpp>
#include <iostream>

CONTRACT_CLASS(
    struct (x) // enclosing class
) {
    CONTRACT_CLASS_INVARIANT( std::cout << "x's invariant" << std::endl )

    CONTRACT_CLASS(
        public struct (y) // nested class
    ) {
        CONTRACT_CLASS_INVARIANT( std::cout << "y's invariant" << std::endl
)

        CONTRACT_FUNCTION(
            public void (f) ( void )
        ) {}
    };
};

int main ( void )
{
    x::y yy;
    yy.f(); // checks y's invariant but not x's invariant
    return 0;
}

In fact in this example an object of x is not even created. Of course, if y
was to create or get an object of x and invoke x's member functions then x's
contracts will be checked as usual.

This makes sense to me but I thought to share it with the ML.

Thanks.
--Lorenzo

--
View this message in context: http://boost.2283326.n4.nabble.com/boost-contract-nothing-special-about-nested-classes-tp4515496p4515496.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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