"end()" iterator for back inserters?
- by Thanatos
For iterators such as those returned from std::back_inserter(), is there something that can be used as an "end" iterator?
This seems a little nonsensical at first, but I have an API which is:
template<typename InputIterator, typename OutputIterator>
void foo(
InputIterator input_begin,
InputIterator input_end,
OutputIterator output_begin,
OutputIterator output_end
);
foo performs some operation on the input sequence, generating an output sequence. (Who's length is known to foo but may or may not be equal to the input sequence's length.)
The taking of the output_end parameter is the odd part: std::copy doesn't do this, for example, and assumes you're not going to pass it garbage. foo does it to provide range checking: if you pass a range too small, it throws an exception, in the name of defensive programming. (Instead of potentially overwriting random bits in memory.)
Now, say I want to pass foo a back inserter, specifically one from a std::vector which has no limit outside of memory constraints. I still need a "end" iterator - in this case, something that will never compare equal. (Or, if I had a std::vector but with a restriction on length, perhaps it might sometimes compare equal?)
How do I go about doing this? I do have the ability to change foo's API - is it better to not check the range, and instead provide an alternate means to get the required output range? (Which would be needed anyways for raw arrays, but not required for back inserters into a vector.) This would seem less robust, but I'm struggling to make the "robust" (above) work.