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
2 answers

Does specialization happen with or without a type annotation?

Documentation: Argument-type declarations normally have no impact on performance: regardless of what argument types (if any) are declared, Julia compiles a specialized version of the function for the actual argument types passed by the caller. For…
user19273273
3
votes
1 answer

C++ overload of swap function not working

I'm writing a custom class for which I want to use the std::swap function. As I read in this post How to overload std::swap() , I have to overload it in the same namespace of the object I'm trying to swap and there's an example. I (thing) I'm…
linamamm
  • 65
  • 6
3
votes
3 answers

Why do these type arguments not conform to a type refinement?

Why does this Scala code fail to typecheck? trait T { type A } trait GenFoo[A0, S <: T { type A = A0 }] trait Foo[S <: T] extends GenFoo[S#A, S] I don't understand why "type arguments [S#A,S] do not conform to trait GenFoo's type parameter bounds…
Kipton Barros
  • 21,002
  • 4
  • 67
  • 80
3
votes
3 answers

Template class with template constructor specialization for initializing a templated base class

I have a template base class with a template parameter of type bool. The constructor parameter list of this base class depends on whether the template parameter is true or false. I want to derive from this class another template class with a…
lexical
  • 76
  • 1
  • 6
3
votes
2 answers

Are specializations over a non-type template parameter with an argument of different pointer-to-members guaranteed to be unique specializations?

Consider the following class template: template struct T {}; Are two specializations of T, where the respective template-argument is a pointer-to-member(1) to two different but same-type members, guaranteed to refer to different…
dfrib
  • 70,367
  • 12
  • 127
  • 192
3
votes
2 answers

How to specialize a variadic template function in c++?

Let the function foo be given with the following "signature": template void foo(Ts ...args) This is a bit overdone since I need foo to proccess doubles only. How can I modify foo so it accepts doubles only? The original code has…
BlueTune
  • 1,023
  • 6
  • 18
3
votes
1 answer

Overload/specialization of a concept

Is there a way to overload/specialize concepts like templates? Consider the following pretty simple case where we just want to flag certain Types as 'simple': // overload/specialization for MyClass - WOULD BE NICE - BUT FAILS template
non-user38741
  • 671
  • 5
  • 19
3
votes
4 answers

Specialize template struct with template class as parameter

I'm trying to shape up my template skills (I know very little) by creating a library containing matrices and operations on those matrices. Basically, I want my matrix to be very strongly typed (datatype and size known at compile-time) and I also…
Stavr0s
  • 98
  • 7
3
votes
2 answers

specializing on a subset of types in a C++ template

I have a question about template specialization in C++, and I am hoping someone here can help. I have a class that has 3 template parameters: template class myClass { public: void myFunc(); }; What I want to do is…
Michael
  • 33
  • 3
3
votes
3 answers

Can you instantiate the unspecialized version of a template and inherit from it inside the specialization?

I am trying to define a Vector as a row/column Matrix. Vector needs some methods which Matrix doesn't have, so I specialized Matrix: template struct Matrix { T data[N][M]; }; template
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
3
votes
2 answers

How to perform partial specialisation when two template parameters are of the same type?

How to partial specialization that two template parameter are same type. How to make this code using second function . #include #include template void Translate(A&& a,B* b){ // make some translate…
3
votes
2 answers

how to add member-variable for a specialized version of a template class?

I have a template class, and at least 95% codes of it is same for all types of the template-parameter, unless a member-variable and a function should be added for one specialization. The sample I want to get is following: template class…
3
votes
1 answer

As template type argument, why doesn't type[N] match its specialized version ---- template class S

Here is a demo (shortened from cppreference): #include #include template struct is_array : std::false_type {}; template struct is_array : std::true_type {}; class A {}; int main() { std::cout…
Chen Li
  • 4,824
  • 3
  • 28
  • 55
3
votes
1 answer

Prevent compilation of unused template specialization in c++

Let's consider a struct Foo, templated by an enum called TYPE: enum TYPE { TYPE_A, TYPE_B }; template struct Foo; Foo is not defined but only specialized twice. One specialization adds a int* to the class while the other adds a…
3
votes
2 answers

C++ template declaration that restricts specializations too much

I'll try to explain my current problem briefly. It could be solved easily but the only solution I've found until now doesn't satisfy myself, maybe you'll indicate to me the next level of indirection that would be needed for resolution. I use CRTP…