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

An Optimised Version of std::fill

Demonstrates a version of std::fill that uses has_trivial_assign to determine whether to use memset to optimise the fill operation (see fill_example.cpp):

//
// fill
// same as std::fill, but uses memset where appropriate
//
namespace detail{

template <typename I, typename T, bool b>
void do_fill(I first, I last, const T& val, const boost::integral_constant<bool, b>&)
{
   while(first != last)
   {
      *first = val;
      ++first;
   }
}

template <typename T>
void do_fill(T* first, T* last, const T& val, const boost::true_type&)
{
   std::memset(first, val, last-first);
}

}

template <class I, class T>
inline void fill(I first, I last, const T& val)
{
   //
   // We can do an optimised fill if T has a trivial assignment 
   // operator and if it's size is one:
   //
   typedef boost::integral_constant<bool, 
      ::boost::has_trivial_assign<T>::value && (sizeof(T) == 1)> truth_type;
   detail::do_fill(first, last, val, truth_type());
}

PrevUpHomeNext