blob: 5e7c5cf6da36039c677fbfd8e6db70f4e1ef06d3 [file] [log] [blame]
Brian Silverman407d3cd2018-08-04 17:48:52 -07001[/
2Copyright 2018 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7]
8
9[section:exchange exchange]
10
11[simplesect Authors]
12
13* Glen Fernandes
14
15[endsimplesect]
16
17[section Overview]
18
19The header <boost/core/exchange.hpp> provides the function template
20`boost::exchange` which is an implementation of the `std::exchange`
21function introduced in C++14.
22
23[endsect]
24
25[section Examples]
26
27The following example shows `boost::exchange` used to simplify the
28implementation of a move constructor.
29
30```
31Node(Node&& other)
32 : head_(boost::exchange(other.head_, nullptr))
33 , tail_(boost::exchange(other.tail_, nullptr)) { }
34```
35
36[endsect]
37
38[section Reference]
39
40```
41namespace boost {
42 template<class T, class U = T>
43 constexpr T exchange(T& t, U&& u);
44}
45```
46[section Functions]
47
48[*`template<class T, class U = T> constexpr T exchange(T& t, U&& u);`]
49
50Equivalent to:
51
52```
53T v = std::move(t);
54t = std::forward<U>(u);
55return v;
56```
57
58[endsect]
59
60[endsect]
61
62[endsect]