Output

Back to Generic programming

An output is any object which can be called like a function, taking a single object as an argument and returning a boolean. In generic functions it works as an abstraction for reporting a sequence of unspecified length back to the caller.

The returned boolean tells the surrounding algorithm whether it should continue reporting or not. This allows for an early exit in the reporting process in case not all information is needed (say, finding the first occurrence of an element in a sequence, instead of all). The algorithm may choose to follow this advice or not.

In C++11, the best way to specify reporters is by lambda functions. The outputs provided here were created under the restrictions of C++98 which does not have lambdas. The user may choose to use either these types or lambdas. In any case, the provided outputs at least work as examples.

Outputs vs output iterators

In C++ STL, output iterators are used to report sequences generically out of functions. However, output iterators have a lot of extraneous functionality which is not essential to the task. For example, its position, or the requirement that to report something through an iterator, it needs to be dereferenced. This is in contradiction to the goal of generic programming; algorithms should work at a maximally wide range of types while minimizing requirements.

Learn more

Files

Output archetype

Output concept