Boost logo

Boost :

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


Hi

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
    {
    public:
        result_type run()
        {
            precondition();
            result_type res = body();
            postcondition();
            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;
    return anImpl.run();
}

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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk