I am using std::transform with an std::back_inserter to append elements to an std::deque. Now the transformation may fail and will return a invalid object (say an uninitialized boost::optional or a null pointer) in some cases. I would like to filter out the invalid objects from getting appended.
I thought about using boost::filter_iterator, but not sure how to present the end() parameter of the filtered range.
The documentation of boost::filter_iterator suggests that output filtering is possible. Should I just specialize operator == for std::back_insert_iterator in this case to always return false?
In addition to this, if I want to append values of initialized boost::optional or pointers, can I chain boost::filter_iterator and boost::indirect_iterator?
I am trying to avoid rolling out my own transform_valid function that takes an optional extractor function.
Is it even possible to use filter_iterator as an output iterator?
I suggest using boost range (algorithms & adaptors) for ease of use, you’d write:
Here is a complete working example of that:
Update
With a little help from this answer: Use boost::optional together with boost::adaptors::indirected
I now include an elegant demonstration of using the
indirectedrange adaptor as well for immediate output of the queue (dereferencing the optionals):