Boost logo

Boost :

From: Martin (m_at_[hidden])
Date: 2002-10-27 03:50:01


Does anybody have any ideas or pointers to how programming by contract can
be implemented easily in C++? I can only think of one non-elegant solution
which involves defining a class in the function that is derived from a
template class. something like this (not fully implemented):

namespace boost
    template<class result_type>
    class contract
        result_type run()
            result_type res = body();
            return res;
        virtual void precondition() {};
        virtual void postcondition() {};
        virtual result_type body(argument_type arg) = 0;

int foo()
    class MyImplementation : public boost::contract<int>
        void body()
            // here put your own code
        // here define a postcondition
        void postcondition()
            // always fail our postconditions
            BOOST_ASSERT(true == false);

    MyImplementation anImpl;

This example does not handle any arguments for the function. Anyway, I would
appreciate any comments or ideas on this topic.

Regards, Martin Bosticky.

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