• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //[n1962_equal
8 #include <boost/contract.hpp>
9 #include <cassert>
10 
11 // Forward declaration because == and != contracts use one another's function.
12 template<typename T>
13 bool operator==(T const& left, T const& right);
14 
15 template<typename T>
operator !=(T const & left,T const & right)16 bool operator!=(T const& left, T const& right) {
17     bool result;
18     boost::contract::check c = boost::contract::function()
19         .postcondition([&] {
20             BOOST_CONTRACT_ASSERT(result == !(left == right));
21         })
22     ;
23 
24     return result = (left.value != right.value);
25 }
26 
27 template<typename T>
operator ==(T const & left,T const & right)28 bool operator==(T const& left, T const& right) {
29     bool result;
30     boost::contract::check c = boost::contract::function()
31         .postcondition([&] {
32             BOOST_CONTRACT_ASSERT(result == !(left != right));
33         })
34     ;
35 
36     return result = (left.value == right.value);
37 }
38 
39 struct number { int value; };
40 
main()41 int main() {
42     number n;
43     n.value = 123;
44 
45     assert((n == n) == true);   // Explicitly call operator==.
46     assert((n != n) == false);  // Explicitly call operator!=.
47 
48     return 0;
49 }
50 //]
51 
52