mirror of
https://github.com/boostorg/contract.git
synced 2026-02-27 04:52:22 +00:00
finished advanced topic section of docs
This commit is contained in:
41
example/features/old_no_macros.cpp
Normal file
41
example/features/old_no_macros.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
#include <boost/contract.hpp>
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
//[old_no_macros
|
||||
template<typename T>
|
||||
class vector {
|
||||
public:
|
||||
virtual void push_back(T const& value, boost::contract::virtual_* v = 0) {
|
||||
// Program old value instead of using `OLDOF(size())` macro.
|
||||
boost::contract::old_ptr<unsigned> old_size =
|
||||
boost::contract::make_old(v, boost::contract::copy_old(v) ?
|
||||
size() : boost::contract::null_old())
|
||||
;
|
||||
|
||||
boost::contract::guard c = boost::contract::public_function(v, this)
|
||||
.postcondition([&] {
|
||||
BOOST_CONTRACT_ASSERT(size() == *old_size + 1);
|
||||
})
|
||||
;
|
||||
|
||||
vect_.push_back(value);
|
||||
}
|
||||
|
||||
/* ... */
|
||||
//]
|
||||
|
||||
unsigned size() const { return vect_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<T> vect_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
vector<int> vect;
|
||||
vect.push_back(123);
|
||||
assert(vect.size() == 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user