Partial template specialization

Partial template specialization is a particular form of class template specialization. Usually used in reference to the C++ programming language, it allows the programmer to specialize only some arguments of a class template, as opposed to explicit specialization, where all the template arguments are provided.


Templates and specialization

Class templates are really meta-classes: they are partial abstract data types that provide instructions to the compiler on how to create classes with the proper data members. For example, the C++ standard containers are class templates. When a programmer uses a vector, one instantiates it with a specific data type, for example, int, string or double. Each type of vector results in a different class in the compiler's object code, each one working with a different data type.

If one knows that a class template will be used with a specific data type fairly often and this data type allows some optimizations (e.g. bit shifting with integers, as opposed to multiplying or dividing by 2), one may introduce a specialized class template with some of the template parameters preset. When the compiler sees such a class template instantiated in code, it will generally choose the most specialized template definition that matches the instantiation. Therefore, an explicit specialization (one where all the template arguments are specified) will be preferred to a partial specialization if all the template arguments match.

Partial specialization

Templates can have more than one parameter type. Some older compilers allow one only to specialize either all or none of the template's parameters. Compilers that support partial specialization allow the programmer to specialize some parameters while leaving the others generic.

Example

Suppose there exists a KeyValuePair class with two template parameters, as follows.

template <typename Key, typename Value>
class KeyValuePair {};

The following is an example of a class that defines an explicit (or full) template specialization of KeyValuePair by pairing integers with strings. The class type retains the same name as the original version.

template <>
class KeyValuePair<int, std::string> {};

The next is an example of partial specialization of KeyValuePair with the same name as the original version and one specialized template parameter.

template <typename Key>
class KeyValuePair<Key, std::string> {};

The next example class KeyStringPair is derived from the original KeyValuePair with a new name, and defines a partial template specialization. In contrast to the explicit specialization above, only the Value template parameter of the superclass is specialized, while the Key template parameter remains generic.

template <typename Key>
class KeyStringPair : public KeyValuePair<Key, std::string> {};

It does not matter which template parameters are specialized and which remain generic. For instance, the following is also a valid example of a partial specialization of the original KeyValuePair class.

template <typename Value>
class IntegerValuePair : public KeyValuePair<int, Value> {};

Caveats

C++ templates are not limited to classes - they can also be used to define function templates. Although function templates can be fully specialized, they cannot be partially specialized, irrespective of whether they are member function templates or non-member function templates. This can be beneficial to compiler writers, but affects the flexibility and granularity of what developers can do.[1] But, function templates can be overloaded, which gives nearly the same effect as what partial function template specialization would have.[2] The following examples are provided to illustrate these points.

// legal: base function template
template <typename ReturnType, typename ArgumentType>
ReturnType Foo(ArgumentType arg);

// legal: explicit/full function template specialization
template <>
std::string Foo<std::string, char>(char arg) { return "Full"; }

// illegal: partial function template specialization of the return type
//          function template partial specialization is not allowed
// template <typename ArgumentType>
// void Foo<void, ArgumentType>(ArgumentType arg);

// legal: overloads the base template for a pointer argument type
template <typename ReturnType, typename ArgumentType>
ReturnType Foo(ArgumentType *argPtr) { return "PtrOverload"; }

// legal: base function name reused. Not considered an overload. ill-formed: non-overloadable declaration (see below)
template <typename ArgumentType>
std::string Foo(ArgumentType arg) { return "Return1"; }

// legal: base function name reused. Not considered an overload. ill-formed: non-overloadable declaration (see below)
template <typename ReturnType>
ReturnType Foo(char arg) { return "Return2"; }

In the example listed above, note that while the last two definitions of the function Foo are legal C++ but is considered ill-formed according to the standard because they are non-overloadable declarations.[3] This is because the definition of function overloading only accounts for the function name, parameter type list and the enclosing namespace (if any). It does not account for the return type.[4] However, these functions can still be called by explicitly indicating the signature to the compiler, as demonstrated by the following program.

// note: to be compiled in conjunction with the definitions of Foo above

int main(int argc, char *argv[])
{
    char c = 'c';
    std::string r0, r1, r2, r3;
    // let the compiler resolve the call
    r0 = Foo(c);
    // explicitly specify which function to call
    r1 = Foo<std::string>(c);
    r2 = Foo<std::string, char>(c);
    r3 = Foo<std::string, char>(&c);
    // generate output
    std::cout << r0 << " " << r1 << " " << r2 << " " << r3 << std::endl;
    return 0;
}

//expected output:
Return1 Return2 Full PtrOverload

References

  1. Alexandrescu, Andrei (1 February 2001). Modern C++ Design. Addison Wesley. p. 23. ISBN 0-201-70431-5.
  2. Sutter, Herb (July 2001). "Why Not Specialize Function Templates?". C/C++ Users Journal. 19 (7). Retrieved 7 December 2014.
  3. "ISO/IEC JTC1 SC22 WG21 N 3690: Programming Languages — C++" (PDF). ISO. 15 May 2013. p. 294. Retrieved 16 October 2016. 13.1 Overloadable declarations [over.load] Not all function declarations can be overloaded. Those that cannot be overloaded are specified here. A program is ill-formed if it contains two such non-overloadable declarations in the same scope.
  4. "ISO/IEC JTC1 SC22 WG21 N 3690: Programming Languages — C++" (PDF). ISO. 15 May 2013. pp. 294–296. Retrieved 16 October 2016. 13.1 Overloadable declarations [over.load]
This article is issued from Wikipedia - version of the 10/15/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.