
On Jan 15, 2013, at 2:13 PM, Thomas Jordan <tomjordan766@gmail.com> wrote: [Please don't overquote.]
Here is the code for the safer version: [snip] //namespace for overloads of move() function namespace Overloads { //Default - move as copy template <typename T> inline void move(T& from, T& to) { T tmp(from); from = to; to = tmp; } }
//namespace for implementation functions namespace SimpleMove_Impl { template <typename T> inline void move_impl(T& from, T& to) { using ::Overloads::move; //bring this into scope move(from, to); } [snip] //convenience function to more between iterators template<typename T> inline void iter_move(T it1, T it2) { ::SimpleMove_Impl::move_impl(*it1, *it2); }
//move algorithm - could also have move_backward similarly template<typename I, typename O> inline O move(I f, I l, O result) { //dispatches to std::copy for scalar return ::SimpleMove_Impl::move_impl(f, l, result, ::boost::is_scalar<std::iterator_traits<I>::value_type>::type()); }
//overload for built-in array template<class T, std::size_t N> inline void move(T (& left)[N], T (& right)[N]) { ::SimpleMove::move(left, (left + N), right); } }
//utility to quickly define move functions for user-defined type in //terms of swap #define MAKE_MOVEABLE_USING_SWAP(X) \ inline void move(X& from)\ {\ this->swap(from);\ }\ inline friend void move(X& from, X& to)\ {\ to.move(from);\ }
//MoveOverloads.h #include <vector> #include <string> #include <boost/array.hpp>
//Add additional overloads you require, for e.g., types in std:: or boost::, to //Overloads namespace in the examples of std:: containers below, //implemented in terms of swap namespace Overloads { template<typename T> inline void move(std::vector<T>& left, std::vector<T>& right) { left.swap(right); }
inline void move(std::string& left, std::string& right) { left.swap(right); }
template<typename T, size_t N> inline void move(boost::array<T,N>& left, boost::array<T,N>& right) { SimpleMove::move(left.begin(), left.end(), right.begin()); } }
This could be useful as it provides a support for basic move semantics via a very small library, enabling 'moving' of your own types via custom move functions and e.g., of C++03 std:: containers via their swap functions, in conjunction with (N)RVO and copy-elision.
Using this for types not *always* "overloaded" by this library means ODR violations. Any type that is sometimes "overloaded", because the right header was included, and other times not leads to different instantiations of the function templates. ___ Rob