encyclopegia.org

Partial template specialization

Partial template specialization is a particular form of class template specialization.[src] 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 full specialization, where all the template arguments are provided.[src]

Partial template specialization is a particular form of class template specialization.[src] 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 full specialization, where all the template arguments are provided.[src] ==Partial specialization== Templates can have more than one parameter type.[src] Some older compilers allow one only to specialize either all or none of the template's parameters.[src] Compilers that support partial specialization allow the programmer to specialize some parameters while leaving the others generic.[src] ===Example=== Suppose there exists a KeyValuePair class with two template parameters, as follows.[src] template class KeyValuePair {}; The following is an example of a class that defines an explicit full template specialization of KeyValuePair by pairing integers with strings.[src] The class type retains the same name as the original version.[src] using std::string; template <> class KeyValuePair {}; The next is an example of partial specialization of KeyValuePair with the same name as the original version and one specialized template parameter.[src] template class KeyValuePair {}; The next example class KeyStringPair is derived from the original KeyValuePair with a new name, and defines a partial template specialization.[src] In contrast to the explicit specialization above, only the V template parameter of the superclass is specialized, while the K template parameter remains generic.[src] template class KeyStringPair : public KeyValuePair {}; It does not matter which template parameters are specialized and which remain generic.[src] For instance, the following is also a valid example of a partial specialization of the original KeyValuePair class.[src] template class IntegerValuePair : public KeyValuePair {}; ===Caveats=== C++ templates are not limited to classes - they can also be used to define function templates.[src] 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.[src] This can be beneficial to compiler writers, but affects the flexibility and granularity of what developers can do.[src] But, function templates can be overloaded, which gives nearly the same effect as what partial function template specialization would have.[src] The following examples are provided to illustrate these points.[src] import std; using std::string; // legal: base function template template Ret Foo(Arg arg); // legal: explicit/full function template specialization template <> string Foo (char arg) { return "Full"; } // illegal: partial function template specialization of the return type // function template partial specialization is not allowed // template // void Foo (Arg arg); // legal: overloads the base template for a pointer argument type template Ret Foo(Arg* argPtr) { return "PtrOverload"; } // legal: base function name reused.[src] Not considered an overload.[src] ill-formed: non-overloadable declaration (see below) template string Foo(Arg arg) { return "Return1"; } // legal: base function name reused.[src] Not considered an overload.[src] ill-formed: non-overloadable declaration (see below) template Ret Foo(char arg) { return "Return2"; } In the example listed above, note that while the last two definitions of the function Foo are legal C++, they are considered ill-formed according to the standard because they are non-overloadable declarations.[src] This is because the definition of function overloading only accounts for the function name, parameter type list and the enclosing namespace (if any).[src] It does not account for the return type.[src] However, these functions can still be called by explicitly indicating the signature to the compiler, as demonstrated by the following program.[src] import std; using std::string; int main(int argc, char* argv[]) { char c = 'c'; // let the compiler resolve the call string r0 = Foo(c); // explicitly specify which function to call string r1 = Foo (c); string r2 = Foo (c); string r3 = Foo (&c); // generate output: // Return1, Return2, Full, PtrOverload std::println("{}, {}, {}, {}", r0, r1, r2, r3); } ==References==[src]

29 sentences · 29 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Partial template specialization." encyclopegia.org, 2026, https://encyclopegia.org/wiki/partial-template-specialization. Accessed 25 November 2026.

Chicago

"Partial template specialization." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/partial-template-specialization.

APA

encyclopegia.org. (2026). Partial template specialization. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/partial-template-specialization

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Template:New Wave (Contest)

stub · references available

Template:Support

stub · references available

Template:Lang-tr

stub · references available

Template:Footy-stub

stub · references available

Template:Location map Iceland

stub · references available

Template:Location map Sweden, 40south

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1352218344 · 0.5 · snapshot enwiki-001352218344

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.