Are standard iterator operations allowed?

Consider a standard iterator where you need to allocate memory to move the data structure. Does the standard allow the iterator to throw an exception if memory cannot be allocated? As an example, consider an input iterator for tree data structures. In this case, to cross the tree, you need to either add or save a pointer to the parent for each node (which will slow down operations that do not require a pointer such as inserting / deleting / searching on the tree) or use the stack to help iterator save pointers to the passed nodes. In this case, the progress of the stack may increase until there is no free memory, and the iterator is forced to quit.

+5
source share
1 answer

Yes, the C ++ iterator method is allowed and, as you indicated, can throw in certain circumstances.

The only class of functions in C ++ that cannot get out is the destructor. Indeed, this is simply by agreement (because it makes certain operations almost impossible for proper operation). Destructors can quit, it's very easy to let them do it.

Separate functions can be marked throw()to prevent them from tossing.

+5
source

All Articles