Partial template specialization

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 specialize the template by specifying another class template that is identical but by specifying the parameter types. 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 matches.

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. Until recently most compilers did not support this feature well (at best) or ignored such specializations (at worst).

At least the GNU, Intel, Comeau, and Microsoft compilers fully support partial specialization of templates.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Template (programming) — Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. Templates are of… …   Wikipedia

  • Specialization (disambiguation) — A specialization may be a course of study or major at an academic institution or may refer to the field that a specialist practices in. In addition, specialization may mean:Biology: * Specialization (functional) * Specialization of organs *… …   Wikipedia

  • Partial evaluation — In computing, partial evaluation is a technique for program optimization by specialization.A computer program, prog , is seen as a mapping of input data into output data::prog : I {static} imes I {dynamic} o OI {static}, the static data , is the… …   Wikipedia

  • C++ — The C++ Programming Language, written by its architect, is the seminal book on the language. Paradigm(s) Multi paradigm:[1] procedural …   Wikipedia

  • Microsoft Visual Studio — Visual Studio 2010 SP1 editing a WPF application Developer(s) …   Wikipedia

  • Generic programming — is a style of computer programming in which algorithms are written in terms of to be specified later types that are then instantiated when needed for specific types provided as parameters and was pioneered by Ada which appeared in 1983. This… …   Wikipedia

  • C++0x — is the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03. The new …   Wikipedia

  • C++11 — C++11, also formerly known as C++0x,[1] is the name of the most recent iteration of the C++ programming language, replacing C++TR1, approved by the ISO as of 12 August 2011.[2] The name is derived from the tradition of naming language versions by …   Wikipedia

  • Comparison of C Sharp and Java — The correct title of this article is Comparison of C# and Java. The substitution or omission of the # sign is because of technical restrictions. Programming language comparisons General comparison Basic syntax Basic instructions …   Wikipedia

  • Organizational conflict — is a state of discord caused by the actual or perceived opposition of needs, values and interests between people working together. Conflict takes many forms in organizations. There is the inevitable clash between formal authority and power and… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”