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

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>

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

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

            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.


View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at