Creator Functions

Creator functions are a widely used technique in conjunction with template programming. There are many examples of creator functions in the STL (e.g., make_pair). Creator functions are convenience functions that take advantage of the fact that the compiler automatically deduces the function templates’ type arguments, while no such automatic deduction exists for class templates.

Each time you create an object of a type that is generated from a class template, you must specify the entire generated type name including all template type arguments. Often, these generated type names are very long and difficult to read and comprehend. Creator functions make life as a template user a lot easier: a creator function creates an object of a type that is generated from a class template without requiring that we type in very long type names.

More precisely, creator functions are function templates, which have the same template type arguments as the class template that describes the type of the object that is to be created. In a way, a creator function is similar to a constructor: the arguments that you pass to a creator function are exactly the arguments that you would otherwise pass to the constructor of the object that is to be created. Since the compiler performs automatic template argument deduction for function templates, you need not specify all the template arguments when calling a creator function; the compiler will deduce them from the arguments passed to the creator function.