Boost.Contract Review Starts Today
Boost.Contract Library is Accepted
Lorenzo Caminitiさん作のBoost.Contractライブラリが採択されました。
Boost.Contractは元々Contract++という名前で開発されていた、契約プログラミング(契約による設計:DbC - Design by Contract)のためのライブラリです。事前条件、事後条件、クラスの不変条件といったものを指定するために使用します。
このライブラリは、C++11に入りそうで入らなかったConceptも一部含んでおり、Boost.ConceptCheckと組み合わせて使用できます。
サンプルコード:
#include <contract.hpp> // This library. #include <boost/concept_check.hpp> #include <vector> #include "pushable.hpp" // Some base class. CONTRACT_CLASS( template( typename T ) requires( boost::CopyConstructible<T> ) // Concepts. class (vector) extends( public pushable<T> ) // Subcontracting. ) { CONTRACT_CLASS_INVARIANT_TPL( empty() == (size() == 0) // More class invariants here... ) public: typedef typename std::vector<T>::size_type size_type; public: typedef typename std::vector<T>::const_reference const_reference; CONTRACT_FUNCTION_TPL( public void (push_back) ( (T const&) value ) override precondition( size() < max_size() // More preconditions here... ) postcondition( auto old_size = CONTRACT_OLDOF size(), // Old-of values. size() == old_size + 1 // More postconditions here... ) ) { vector_.push_back(value); // Original function body. } // Rest of class here (possibly with more contracts)... public: bool empty ( void ) const { return vector_.empty(); } public: size_type size ( void ) const { return vector_.size(); } public: size_type max_size ( void ) const { return vector_.max_size(); } public: const_reference back ( void ) const { return vector_.back(); } private: std::vector<T> vector_; };