Contract++

Released Contract Programming Library on SourceForge


Boostに提案予定の契約プログラミング(Contract Programming)ライブラリ。
関数の事前条件、事後条件、クラスの不変条件を記述できる。

#include "pushable.hpp" // Some base class (for subcontracting).
#include <contract.hpp> // This library.
#include <vector>       // C++ STL vector.

// Wrapper class that adds contracts to std::vector.
template<typename T>
class myvector: public pushable<T> {

    CONTRACT_INVARIANT( ({
        CONTRACT_ASSERT( ((size() == 0) == empty() );
        ... // More invariants.
    }) ) 
    
public:
    void push_back(const T& element)
    CONTRACT_FUNCTION( (class) (copyable)(myvector)  // Function signature.
            (inherit)(pushable<T>) 
            (public) (void) (push_back)( (const T&)(element) )
    (precondition)({ 
        CONTRACT_ASSERT( size() < max_size() );
        ... // More preconditions.
    })
    (postcondition)({
        CONTRACT_ASSERT( size() == (CONTRACT_OLDOF(this)->size() + 1) ); 
        ... // More postconditions.
    })
    (body)({ 
        vector_.push_back(element); // Original implementation.
    }) ) 
    
    ... // Rest of the class.
private:
    std::vector<T> vector_;
};

このコードは書きたくないなー。