File: if_constexpr.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (113 lines) | stat: -rw-r--r-- 2,906 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

// Copyright (C) 2008-2019 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html

#include <boost/contract.hpp>
#include <boost/type_traits/has_equal_to.hpp>
#include <utility>
#include <cassert>

//[if_constexpr
template<typename T>
void swap(T& x, T& y) {
    constexpr bool b = boost::contract::is_old_value_copyable<T>::value &&
            boost::has_equal_to<T>::value;
    boost::contract::old_ptr<T> old_x, old_y;
    if constexpr(b) { // Contract requires copyable T...
        old_x = BOOST_CONTRACT_OLDOF(x);
        old_y = BOOST_CONTRACT_OLDOF(y);
    }
    boost::contract::check c = boost::contract::function()
        .postcondition([&] {
            if constexpr(b) { // ... and T with `==`...
                BOOST_CONTRACT_ASSERT(x == *old_y);
                BOOST_CONTRACT_ASSERT(y == *old_x);
            }
        })
    ;

    T t = std::move(x); // ...but body only requires movable T.
    x = std::move(y);
    y = std::move(t);
}
//]

struct i { // Non-copyable but has operator==.
    explicit i(int n) : n_(n) {}

    i(i const&) = delete; // Non-copyable.
    i& operator=(i const&) = delete;

    i(i const&& o) : n_(o.n_) {}
    i& operator=(i const&& o) { n_ = o.n_; return *this; }

    friend bool operator==(i const& l, i const& r) { // Operator==.
        return l.n_ == r.n_;
    }

private:
    int n_;
};

struct j { // Copyable but no operator==.
    explicit j(int n) : n_(n) {}

    j(j const& o) : n_(o.n_) {} // Copyable.
    j& operator=(j const& o) { n_ = o.n_; return *this; }

    j(j const&& o) : n_(o.n_) {}
    j& operator=(j const&& o) { n_ = o.n_; return *this; }

    // No operator==.

private:
    int n_;
};

struct k { // Non-copyable and no operator==.
    explicit k(int n) : n_(n) {}

    k(k const&) = delete; // Non-copyable.
    k& operator=(k const&) = delete;

    k(k const&& o) : n_(o.n_) {}
    k& operator=(k const&& o) { n_ = o.n_; return *this; }

    // No operator==.

private:
    int n_;
};

int main() {
    { // Copyable and operator== (so checks postconditions).
        int x = 123, y = 456;
        swap(x, y);
        assert(x == 456);
        assert(y == 123);
    }
    
    { // Non-copyable (so does not check postconditions).
        i x{123}, y{456};
        swap(x, y);
        assert(x == i{456});
        assert(y == i{123});
    }
    
    { // No operator== (so does not check postconditions).
        j x{123}, y{456};
        swap(x, y);
        // Cannot assert x and y because no operator==.
    }
    
    { // Non-copyable and no operator== (so does not check postconditions).
        k x{123}, y{456};
        swap(x, y);
        // Cannot assert x and y because no operator==.
    }
    
    return 0;
}