If I’m making a method that needs to fill a buffer (for example, a wrapper for fread), and under some circumstances (determined at runtime) it can make a good guess at the required buffer size, what should I pass to this method in order to take advantage of this opportunity, while still allowing any reasonable kind of buffer and without requiring multiple calls from the user?
-
I’d like to use an
insert_iterator, but then I can’t tell it to reserve a certain amount of memory. -
On the other hand, if I pass a container as a templated parameter, I’d run into trouble because containers don’t have a standard interface! (e.g.
vectorandstringhavereserve, butdequeandlistdon’t. Or a container might not havepush_back— I don’t believe I’ve seen that being mandatory. etc.)
So what is the appropriate thing to do in a situation like this?
You could create very generic function that takes iterators, then create a family of more specialized functions that take the various sequence container types (vector, list, deque, string), and take appropriate action before forwarding to the more general function, for example: