I have a function that needs to enumerate an iterator multiple times, but according to MSDN, “Once you increment any copy of an input iterator, none of the other copies can safely be compared, dereferenced, or incremented thereafter.”
So to make things easier, instead of creating a separate implementation for non-forward-iterators that copies the data and enumerates the copy, I want to restrict my method to only taking in forward iterators, and rejecting input iterators statically.
Right now I have something like:
template<typename It, typename TCallback /*signature: bool(value_type)*/>
bool EnumerateTwice(const It &begin, const It &end, TCallback callback)
{
for (It it = begin; it != end; ++it)
if (!callback(*it))
return false;
for (It it = begin; it != end; ++it)
if (!callback(*it))
return false;
return true;
}
but nothing restricts It to being a forward iterator.
How do I place that restriction on the templated function? (C++03)
You can use SFINAE and replace
boolby:You may need to define
is_sameandenable_ifyourself if you don’t want to pull them from Boost or TR1: