Tag Archives: template template

Template Template Parameter Overloading

As the ISO C++ committee deliberates on what to include in the next iteration of the language, C++14, I’d submit the following for their consideration…

Often, the need arises to decorate one type with another (typically through derivation or some variation on CRTP). Template Template Parameters are a powerful tool for implementing this, as Alexandrescu demonstrates with his scattered and linear hierarchy generation (Modern C++ Design). The problem is that currently, C++ demands a uniform interface for such parameters – they must have identical parameter lists, despite the fact that “regular” templates and run-time functions offer support for both overloading on parameter lists and for defaulting parameter values.  

There does not appear to be a sound reason for this limitation, as all ambiguity would necessarily be resolved at compile time. This is particularly onerous when using third-party templates which make heavy use of parameter defaulting themselves (e.g., collections) as template parameters.  The defaulting nature is not transparently supported by the referencing template.  One possible implementation might be to use variadic templates.  Another would be to use template typedefs (aliases) to “hide” the optional parameters.  These feel more like workarounds.  My preference would be for the language to simply support this capability natively.  C++ may never achieve the homoiconicity of functional languages, with the unification of compile-time and run-time features, but it can take steps to remove arbitrary distinctions.