More C++ Idioms/Object Generator

= Object Generator =

Intent
To simplify creation of objects without explicitly specifying their types. (This is not the factory method pattern)

Motivation
In C++ template programming, types of objects can get really large and incomprehensible even in small programs. For example, the following type (Wrapper) is a standard unary function object that wraps the member function read_line in class File.

Reading a collection of files using the for_each STL algorithm, without object generators, looks like the following:

The above code is pretty much unreadable and more bloated than necessary. Even typedefs don't improve readability as placeholder typedefs like Wrapper are distracting. The object generator idiom alleviates the situation.

Solution and Sample Code
In the object generator idiom, a template function is created whose only job is to construct a new object from its parameters. It is based on a useful property of function templates which class templates don't have: The type parameters of a function template are deduced automatically from its actual parameters. For example, consider a simple object generator defined in STL: make_pair.

make_pair returns an instance of the pair template depending on the actual parameters of the make_pair function. For example, make_pair(1,1.0) creates an object of type: std::pair by automatically deducing the types of the objects being passed to the object generator function. make_pair is particularly handy in the case where a generated pair object does not need to be stored in a local variable.

The C++ standard library defines several object generators to avoid code bloat. std::bind2nd and std::mem_fun are two such standard object generators that can be used to avoid code bloat in the example shown in the motivation section above.

Known Uses
C++ standard library (mem_fun, make_pair, bind1st, bind2nd etc.)

Related Idioms
Type Generator