• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright (C) 2008-2019 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 #include <boost/contract.hpp>
8 #include <boost/type_traits/has_equal_to.hpp>
9 #include <utility>
10 #include <cassert>
11 
12 //[if_constexpr
13 template<typename T>
swap(T & x,T & y)14 void swap(T& x, T& y) {
15     constexpr bool b = boost::contract::is_old_value_copyable<T>::value &&
16             boost::has_equal_to<T>::value;
17     boost::contract::old_ptr<T> old_x, old_y;
18     if constexpr(b) { // Contract requires copyable T...
19         old_x = BOOST_CONTRACT_OLDOF(x);
20         old_y = BOOST_CONTRACT_OLDOF(y);
21     }
22     boost::contract::check c = boost::contract::function()
23         .postcondition([&] {
24             if constexpr(b) { // ... and T with `==`...
25                 BOOST_CONTRACT_ASSERT(x == *old_y);
26                 BOOST_CONTRACT_ASSERT(y == *old_x);
27             }
28         })
29     ;
30 
31     T t = std::move(x); // ...but body only requires movable T.
32     x = std::move(y);
33     y = std::move(t);
34 }
35 //]
36 
37 struct i { // Non-copyable but has operator==.
ii38     explicit i(int n) : n_(n) {}
39 
40     i(i const&) = delete; // Non-copyable.
41     i& operator=(i const&) = delete;
42 
ii43     i(i const&& o) : n_(o.n_) {}
operator =i44     i& operator=(i const&& o) { n_ = o.n_; return *this; }
45 
operator ==(i const & l,i const & r)46     friend bool operator==(i const& l, i const& r) { // Operator==.
47         return l.n_ == r.n_;
48     }
49 
50 private:
51     int n_;
52 };
53 
54 struct j { // Copyable but no operator==.
jj55     explicit j(int n) : n_(n) {}
56 
jj57     j(j const& o) : n_(o.n_) {} // Copyable.
operator =j58     j& operator=(j const& o) { n_ = o.n_; return *this; }
59 
jj60     j(j const&& o) : n_(o.n_) {}
operator =j61     j& operator=(j const&& o) { n_ = o.n_; return *this; }
62 
63     // No operator==.
64 
65 private:
66     int n_;
67 };
68 
69 struct k { // Non-copyable and no operator==.
kk70     explicit k(int n) : n_(n) {}
71 
72     k(k const&) = delete; // Non-copyable.
73     k& operator=(k const&) = delete;
74 
kk75     k(k const&& o) : n_(o.n_) {}
operator =k76     k& operator=(k const&& o) { n_ = o.n_; return *this; }
77 
78     // No operator==.
79 
80 private:
81     int n_;
82 };
83 
main()84 int main() {
85     { // Copyable and operator== (so checks postconditions).
86         int x = 123, y = 456;
87         swap(x, y);
88         assert(x == 456);
89         assert(y == 123);
90     }
91 
92     { // Non-copyable (so does not check postconditions).
93         i x{123}, y{456};
94         swap(x, y);
95         assert(x == i{456});
96         assert(y == i{123});
97     }
98 
99     { // No operator== (so does not check postconditions).
100         j x{123}, y{456};
101         swap(x, y);
102         // Cannot assert x and y because no operator==.
103     }
104 
105     { // Non-copyable and no operator== (so does not check postconditions).
106         k x{123}, y{456};
107         swap(x, y);
108         // Cannot assert x and y because no operator==.
109     }
110 
111     return 0;
112 }
113 
114