Reputation: 348
I am developing a set of functions that takes advantage of containers that have packed and sequential memory storage (for memory copies). They have function signatures in the style of most STD functions, input/output iterators point to elements and denote ranges. For instance, a function could look like this:
template< typename InputIterator, typename OutputIterator >
OutputIterator fooBar( InputIterator& first, InputIterator& last,
OutputIterator& result );
I wish to verify that the iterators passed are legal, that is packed and sequential. For the STD containers, this is limited to std::vector and std::array. Unfortunately, I can't rely on the iterator 'category' trait, because the random access trait does not imply seqential storage. An example of this is microsofts concurrent_vector class, documented here parallel containers
In addition, I can't accept all iterators from the vector and array classes either, for instance i need to reject reverse iterators, and std::vector<bool
> iterators are unsuitable because of the proxy class that it uses.
I've attempted to create my own traits class to distinguish and filter the iterators with the constraints that i describe above, but i'm running into template syntax problems. I am looking for feedback from others on how they would approach this problem.
Thanks
Upvotes: 1
Views: 342
Reputation: 131799
I don't think you can do this. Iterators are an abstraction whose whole purpose is to make the iteration process independent of the underlying architecture. There is no information in the standard iterators that denote the underlying memory structure or even anything remotely similar.
On your std-algorithm-like functions, it's generally advised to pass iterators by value, since they should be cheap / small objects. It should be especially noted that your function would never be able to be called as fooBar(c.begin(), c.end(), some_out_it);
, since it takes the the input iterators by reference-to-non-const.
As a last point, you can filter out reverse iterators by testing whether the iterator type is a specialization of std::reverse_iterator<Iter>
, since atleast the Container::(const_)reverse_iterator
type of standard containers are required to be one.
Upvotes: 1