...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Demonstrates a version of std::copy
that uses has_trivial_assign
to determine whether to use memcpy
to optimise the copy operation (see copy_example.cpp):
// // opt::copy // same semantics as std::copy // calls memcpy where appropriate. // namespace detail{ template<typename I1, typename I2, bool b> I2 copy_imp(I1 first, I1 last, I2 out, const boost::integral_constant<bool, b>&) { while(first != last) { *out = *first; ++out; ++first; } return out; } template<typename T> T* copy_imp(const T* first, const T* last, T* out, const boost::true_type&) { memmove(out, first, (last-first)*sizeof(T)); return out+(last-first); } } template<typename I1, typename I2> inline I2 copy(I1 first, I1 last, I2 out) { // // We can copy with memcpy if T has a trivial assignment operator, // and if the iterator arguments are actually pointers (this last // requirement we detect with overload resolution): // typedef typename std::iterator_traits<I1>::value_type value_type; return detail::copy_imp(first, last, out, boost::has_trivial_assign<value_type>()); }