exchange.qbk 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. [/
  2. Copyright 2018 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:exchange exchange]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/exchange.hpp> provides the function template
  13. `boost::exchange` which is an implementation of the `std::exchange`
  14. function introduced in C++14. `boost::exchange(o, v)` replaces the
  15. value of `o` with `v` and returns the old value of `o`.
  16. [endsect]
  17. [section Examples]
  18. The following example shows `boost::exchange` used to simplify the
  19. implementation of a move constructor.
  20. ```
  21. Node(Node&& other)
  22. : head_(boost::exchange(other.head_, nullptr))
  23. , tail_(boost::exchange(other.tail_, nullptr)) { }
  24. ```
  25. [endsect]
  26. [section Reference]
  27. ```
  28. namespace boost {
  29. template<class T, class U = T>
  30. constexpr T exchange(T& t, U&& u);
  31. }
  32. ```
  33. [section Functions]
  34. [*`template<class T, class U = T> constexpr T exchange(T& t, U&& u);`]
  35. Equivalent to:
  36. ```
  37. T v = std::move(t);
  38. t = std::forward<U>(u);
  39. return v;
  40. ```
  41. [endsect]
  42. [endsect]
  43. [endsect]