1 2 // Copyright (C) 2008-2018 Lorenzo Caminiti 3 // Distributed under the Boost Software License, Version 1.0 (see accompanying 4 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt). 5 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html 6 7 // Test forcing compiler error for old values of non-copyable types. 8 9 #include "if_copyable.hpp" 10 #include <boost/contract/function.hpp> 11 #include <boost/contract/old.hpp> 12 #include <boost/contract/check.hpp> 13 #include <boost/contract/assert.hpp> 14 #include <boost/noncopyable.hpp> 15 16 17 template<typename T> next(T & x)18void next(T& x) { 19 boost::contract::old_ptr<T> old_x = BOOST_CONTRACT_OLDOF(x); 20 boost::contract::check c = boost::contract::function() 21 .postcondition([&] { 22 // No need to check `if(old_x) ...` here. 23 BOOST_CONTRACT_ASSERT(x == *old_x + T(1)); 24 #ifdef BOOST_CONTRACT_NO_ALL 25 #error "force error if no contracts (ASSERT expands to nothing)" 26 #endif 27 }) 28 ; 29 ++x; 30 } 31 main()32int main() { 33 int i = 1; // Test built-in copyable type. 34 cp c(1); // Test custom copyable type. 35 ncp n(1); // Test non-copyable type. 36 37 next(i); // OK. 38 next(c); // OK. 39 next(n); // Error. 40 41 return 0; 42 } 43 44