Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext

Move algorithms

The standard library offers several copy-based algorithms. Some of them, like std::copy or std::uninitialized_copy are basic building blocks for containers and other data structures. This library offers move-based functions for those purposes:

template<typename I, typename O> O move(I, I, O);
template<typename I, typename O> O move_backward(I, I, O);
template<typename I, typename F> F uninitialized_move(I, I, F);
template<typename I, typename F> F uninitialized_copy_or_move(I, I, F);

The first 3 are move variations of their equivalent copy algorithms, but copy assignment and copy construction are replaced with move assignment and construction. The last one has the same behaviour as std::uninitialized_copy but since several standand library implementations don't play very well with move_iterators, this version is a portable version for those willing to use move iterators.

#include "movable.hpp"
#include <cassert>
#include <boost/aligned_storage.hpp>

int main()
{
   const std::size_t ArraySize = 10;
   movable movable_array[ArraySize];
   movable movable_array2[ArraySize];
   //move
   boost::move(&movable_array2[0], &movable_array2[ArraySize], &movable_array[0]);
   assert(movable_array2[0].moved());
   assert(!movable_array[0].moved());

   //move backward
   boost::move_backward(&movable_array[0], &movable_array[ArraySize], &movable_array2[ArraySize]);
   assert(movable_array[0].moved());
   assert(!movable_array2[0].moved());

   //uninitialized_move
   boost::aligned_storage< sizeof(movable)*ArraySize
                         , boost::alignment_of<movable>::value>::type storage;
   movable *raw_movable = static_cast<movable*>(static_cast<void*>(&storage));
   boost::uninitialized_move(&movable_array2[0], &movable_array2[ArraySize], raw_movable);
   assert(movable_array2[0].moved());
   assert(!raw_movable[0].moved());
   return 0;
}


PrevUpHomeNext