thrust::counting_iterator as OutputIterator

I have a function (composition of Thrust algorithms) templateized by OutputIterator type of obeg parameter. Initial length of output container is unknown.

template< typename InputIterator, typename OutputIterator >
OutputIterator f(InputIterator beg, InputIterator end, OutputIterator obeg)
    return thrust::algorithm(temp.cbegin(), temp.cend(), obeg);

What I want is to call this function first time to determine the required length of obeg sequence. Then I want to allocate output vector of the length. After all I want to call f again, but pass output.begin() as obeg. The function is pure.

Can I use thrust::counting_iterator as output iterator to determine the length of output of an algorithm? Can it behave just like thrust::discard_iterator with respect to assignment to dereferenced value (which should be like a std::ignore from , but with operator Incrementable () const overloading)? I need only its counting capabilities in the case.

no, I don’t believe it works that way

many thrust algorithms have methods to determine the output size, e.g. by returning an iterator to the last item. The usual approach would be to pass a discard iterator to that algorithm, and then determine the output size that way.

I recognize that your function is not a single thrust algorithm, but my suggestion would be to find a way to construct your composite function in such a way that you can pass a discard iterator and return the size somehow.

Do you mean, that thrust::distance between thrust::discard_iterators is meaningful?

Thank you.