Questions tagged [specialization]

A powerful feature of C++'s templates is `template specialization`. This allows alternative implementations to be provided based on certain characteristics of the parameterized type that is being instantiated. Template specialization has two purposes: to allow certain forms of optimization, and to reduce code bloat.

This tag can refer to specialization in C++ or GHC, a Haskell compiler.

C++

A powerful feature of C++'s templates is template specialization. This allows alternative implementations to be provided based on certain characteristics of the parameterized type that is being instantiated.

Template specialization has two purposes:

  • to allow certain forms of optimization
  • to reduce code bloat.

For example, consider a sort() template function. One of the primary activities that such a function does is to swap or exchange the values in two of the container's positions.

  • If the values are large (in terms of the number of bytes it takes to store each of them), then it is often quicker to first build a separate list of pointers to the objects, sort those pointers, and then build the final sorted sequence.

  • If the values are quite small however it is usually fastest to just swap the values in-place as needed.

Furthermore if the parameterized type is already of some pointer-type, then there is no need to build a separate pointer array.

Template specialization allows the template creator to write different implementations and to specify the characteristics that the parameterized type(s) must have for each implementation to be used.

GHC

GHC provides a mechanism to specialize the type of polymorphic functions. This has the effect of removing dictionary lookups, and thus can improve code performance.

GHC performs some specializations automatically, but also provides a SPECIALIZE pragma that directs the compiler to specialize a function to the given type signature.

519 questions
9
votes
2 answers

G++ generates code for unused template specializations?

In a bit of serialization code for a project I'm working on I have a type whose size is compiler dependent. In order to deal with this, I decided to use a template specialization, which works great. Everything is resolved at compile time. The code…
808140
  • 205
  • 2
  • 11
8
votes
1 answer

How can I specialize low-level functions for performance while keeping high-level functions polymorphic?

I extracted the following minimal example from my production project. My machine learning project is made up of a linear algebra library, a deep learning library, and an application. The linear algebra library contains a module for matrices based on…
Jules
  • 487
  • 3
  • 10
8
votes
3 answers

Partial specialization of double-templated method fails

There is the template class List. template class List { public: template void load ( const char *file); ... }; template template
Robo
  • 311
  • 5
  • 10
8
votes
1 answer

Class Table Inheritance vs. Denormalization

I'm trying to model a specialization/generalization, leaning towards using class table inheritance (see this answer). However, my co-worker has maintenance and performance concerns because there will be many (50+) overlapping specializations of the…
user285498
8
votes
1 answer

Template specialization when parameter values are equal

I have a function of the form template void f(); That I'd like to specialize when a == b. Pseudocode looks something like: template void f(){ //something} template void f(){ //something…
Ben Jones
  • 919
  • 1
  • 8
  • 22
8
votes
3 answers

ambiguous template weirdness

I have the following code (sorry for the large code chunk, but I could not narrow it down any more) template struct enable_if_c { typedef void type; }; template <> struct enable_if_c {}; template struct enable_if…
keis
  • 113
  • 7
7
votes
2 answers

Templated function template specialization

I want to write a specialization for a template function, where the type for which it is specialized is itself a templated type. (I am using C++11 or higher.) In the example code below, I have the generic function convertTo and a working…
lawilog
  • 173
  • 6
7
votes
3 answers

Type-based templating function in C++

I want to write a function that fail-safe accesses std::map. At many places in my code I want to access a std::map by key, but in case the key does not exist, I want to have a kind of default value instead of an exception (which is a lot of code for…
Schubi Duah
  • 309
  • 1
  • 7
7
votes
2 answers

static member function inside class template specialization

I am struggling to access static member function defined inside class template. In the header file TemplateTest.h I defined the primary class Template as: #include template struct TemplateTest { public: void static…
jazaman
  • 1,007
  • 3
  • 12
  • 30
7
votes
2 answers

template specialization of template class

I want to specialize following member function: class foo { template T get() const; }; To other class bar that depends on templates as well. For example, I would like bar to be std::pair with some template parameters, something…
Artyom
  • 31,019
  • 21
  • 127
  • 215
7
votes
4 answers

Template typedef for std container (without specialization)?

Is it possible to use typedef on a std container without specializing it? Code like this works: typedef std::vector intVector; But for this code: template typedef std::vector DynamicArray; I get an error: template…
dtech
  • 47,916
  • 17
  • 112
  • 190
6
votes
3 answers

Template specialization by another template (of same class)

I'm writing an array class. This array class can contain again arrays as members. When implementing a printing function, I need specializations. 26:template class array : public vector{ public: ... string* printToString(); …
6
votes
2 answers

block non-specialized template c++

Is it possible to somehow forbid using templated function for types for which specialization was not explicitly written. I mean something like that template void foo(){} template <> void foo(){} int main(int argc, char* argv[]){ …
j_kubik
  • 6,062
  • 1
  • 23
  • 42
6
votes
3 answers

How can I make classes easily configurable without run-time overhead?

I recently started playing with Arduinos, and, coming from the Java world, I am struggling to contend with the constraints of microcontroller programming. I am slipping ever closer to the Arduino 2-kilobyte RAM limit. A puzzle I face constantly is…
Boann
  • 48,794
  • 16
  • 117
  • 146
6
votes
1 answer

Having trouble with the end of this cppreference.com article

I have read What does template's implicit specialization mean? and its answers, but I am still not satisfied that I understand this part of Partial template specialization from cppreference.com: If the primary member template is explicitly (fully)…
TRPh
  • 187
  • 1
  • 9