Brian Silverman | 407d3cd | 2018-08-04 17:48:52 -0700 | [diff] [blame^] | 1 | [/ |
| 2 | Copyright 2018 Glen Joseph Fernandes |
| 3 | (glenjofe@gmail.com) |
| 4 | |
| 5 | Distributed 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 | |
| 19 | The header <boost/core/exchange.hpp> provides the function template |
| 20 | `boost::exchange` which is an implementation of the `std::exchange` |
| 21 | function introduced in C++14. |
| 22 | |
| 23 | [endsect] |
| 24 | |
| 25 | [section Examples] |
| 26 | |
| 27 | The following example shows `boost::exchange` used to simplify the |
| 28 | implementation of a move constructor. |
| 29 | |
| 30 | ``` |
| 31 | Node(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 | ``` |
| 41 | namespace 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 | |
| 50 | Equivalent to: |
| 51 | |
| 52 | ``` |
| 53 | T v = std::move(t); |
| 54 | t = std::forward<U>(u); |
| 55 | return v; |
| 56 | ``` |
| 57 | |
| 58 | [endsect] |
| 59 | |
| 60 | [endsect] |
| 61 | |
| 62 | [endsect] |