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
3
votes
1 answer

Specialization of singleton parameters

I'm playing around with specialization of singletons: {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} module Data.Test where data SingBool (b :: Bool) where STrue :: SingBool 'True SFalse ::…
Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
3
votes
1 answer

How does trait specialization actually work?

I tried to specialize a trait, and it fails to compile because of "conflicting implementations". But my understanding of specialization is that more specific implementations should override more generic ones. Here is a very basic example: mod diving…
SpamapS
  • 1,087
  • 9
  • 15
3
votes
1 answer

C++ partial template specialization syntax

for primary template: template class MyClass {... with template specialization, what is the difference between template class MyClass {... and template<> class MyClass {...
uray
  • 11,254
  • 13
  • 54
  • 74
3
votes
2 answers

Why is std::min_element and company not specialized for std::vector

I have a std::vector instance (foo, say), and I need to write a function that returns true if all the elements are true. I use return *std::min_element(foo.begin(), foo.end()); to achieve that, but this has got me thinking: you know the…
Paul Logue
  • 806
  • 6
  • 10
3
votes
2 answers

implicit instantiation of undefined template when instantiating alias of partial template spec

I'm following the sample templates within the book 'Practical C++ Metaprogramming' and have reached a part of the sample where I cannot get the code to compile without bypassing the alias. When using the alias make_tuple_of_derefed_params_t I…
David Ryan
  • 81
  • 1
  • 6
3
votes
1 answer

Template class template constructor specialization

I have a template class with a member whose type depends on the class' template argument. The class has a template constructor. How do I specialize the constructor for different cases of the class' template argument that determines the type of the…
SU3
  • 5,064
  • 3
  • 35
  • 66
3
votes
1 answer

C++ template specialization

Hello! Does someone know a way to achieve or emulate the following behaviour? (this code results in compilation-time error). E.g, I want to add specific template specialization only in derived classes. struct Base { template void…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
3
votes
2 answers

C++ specialise function on enum

Is it possible to specialise a template function on an enum? I've seen noted here a template function can be disabled if it isn't an enum, but is this possible whilst still allowing other types? My example below shows specialisations for int, float,…
c z
  • 7,726
  • 3
  • 46
  • 59
3
votes
2 answers

Define a Specialization As Instantiation of Another Class

I have a situation where I would like to define a specialization to be the same as the instantiation of another class. Here's a simple example of what I want (a full intantiation in this example; in my actual problem, I want a partial…
geometrian
  • 14,775
  • 10
  • 56
  • 132
3
votes
3 answers

Declaring an instance of an explicit specializtion of a template within a regular class

I can't get this to compile at all. I may not be possible but I don't know why it should not be. class A { template class B { int test() { return 0; } }; //template <> class B; <-with this, namepace error B
user438938
  • 71
  • 4
3
votes
2 answers

Does order matter in specialization of a function in class template

Consider something like... template class Vector { ... bool operator==( const Vector &rhs ) { // compare and return } bool operator==( const Vector &rhs ) { // compare and return } ... }; Notice how the…
floogads
  • 271
  • 2
  • 4
  • 13
3
votes
3 answers

How can I get a specialized template to use the unspecialized version of a member function?

Consider the following code: template struct vec { vec normalize(); }; template <> struct vec<3> { vec cross_product(const vec& second); vec normalize(); }; template vec vec::normalize() { // code to…
Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56
3
votes
2 answers

C++ Template Specialization Compilation

I'm going to outline my problem in detail to explain what I'm trying to achieve, the question is in the last paragraph if you wish to ignore the details of my problem. I have a problem with a class design in which I wish to pass a value of any type…
3
votes
1 answer

Specializing a function of a template class

This is legal in C++: template class A { void bar() {std::cout << N << '\n';} }; template<> void A<2>::bar() {std::cout << "Two\n";} // This is ok. Now consider this class: template struct B; template
prestokeys
  • 4,817
  • 3
  • 20
  • 43
3
votes
1 answer

Template using directive specialization

While template specialization is allowed, one can't specialize a template using directive. What are some tricks to achieve it anyway ? e.g : this is ok : template class MyTemplate { ... }; template <> class MyTemplate { ...…
Julien__
  • 1,962
  • 1
  • 15
  • 25