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

Why is template specialization of variadic templates different from specialization of non-variadic templates?

I'm don't understand why template specialization is different for variadic templates than for regular (i.e., non-variadic) templates. For example I have a template and a specialization like: enum class MF : int { ZERO = 0, ONE = 1, TWO =…
jlconlin
  • 14,206
  • 22
  • 72
  • 105
0
votes
0 answers

Do functions in template specialization by value also need to have in their declaration?

I am writing a class that has template specialization by value and was wondering if the Constructor and the methods in the template specialization also need to have < VALUE > in their declaration. In other words, which of the following two examples…
raovgarimella
  • 166
  • 1
  • 8
0
votes
3 answers

how do I use type_traits or template function specialization to consolidate template methods

I am trying to consolidate a number of very similar function methods from a class similar to the one shown below and I thought that the best way to efficiently implement this, would be through the use templates coupled with either template function…
johnco3
  • 2,401
  • 4
  • 35
  • 67
0
votes
3 answers

unable to specialize template function for both vector and abstract class

I'm trying to write a template function, but I have trouble specializing it for vector<> and another class at the same time. Here is the code I'm using : // template definition template< class T > void f( const T& value) { cout << "DEFAULT" <<…
0
votes
4 answers

partial specialization variadic template typename as void

I have a function,in a library, that is a variadic template, and is used by a other programme. 1 A.hpp class A { template static Ret f(int id,Args&& ... args); }; #include "A.tpl" A.tpl template
Krozark
  • 862
  • 9
  • 27
0
votes
1 answer

c++11 partial class specialization using already implemented methods

assuimng this example code #include #include #include #include template< typename T, typename S > class MyClass { public: MyClass () : v(10) {} bool ok () { return true; } T run (S s, int i) {…
petrbel
  • 2,428
  • 5
  • 29
  • 49
0
votes
2 answers

Partial template specialization not giving proper results

So I have this class template class mat_base; And I have this specialization template class mat_base; and whenever I try to use…
RamblingMad
  • 5,332
  • 2
  • 24
  • 48
0
votes
1 answer

Omitting class template methods based on template parameter

I've seen various incarnations of my question answered/replied too but I'm still having a hard time figuring out how to omit functions my compiler states are ambiguous. I have a class that is designed to handle streams of data. I have overloaded =…
Eric
  • 1,697
  • 5
  • 29
  • 39
0
votes
1 answer

Function specialization for arrays not being called

I have a function which is templated on its argument: template void F(Vector& vec); I want to add a specialization of this function for numeric arrays. My attempt looks like this: template void F(NumType array[]); I…
selecsosi
  • 161
  • 1
  • 3
  • 11
0
votes
1 answer

too few template-parameter-lists -- specialization of template method, g++

Porting code from gcc 2.95.3 to gcc 4.4.2 results in a new compile time error: too few template-parameter-lists Below is an abstracted and simplified example of that code. The error occurs on the marked line. #include using namespace…
0
votes
3 answers

Specializing a template class member function by type

I'm trying to specialize a member function on a templated class by a type trait of it's template parameter, but my forward declaration is apparently incorrect. Is there an easy fix? #include template class…
Ben Jones
  • 919
  • 1
  • 8
  • 22
0
votes
5 answers

Overloading based on specialization in C++

I'm trying to create a function which is overloaded based on the specialization of its parameter, such as this: class DrawableObject...; class Mobile : public DrawableObject...; class Game { AddObject(DrawableObject * object) { //…
Schtee
0
votes
2 answers

List of prime factors at compile-time: specialization error

Consider the following code (link to IDEONE): #include #include // List of factors template struct factors { }; // Declaration template struct factorization; // Initial…
Vincent
  • 57,703
  • 61
  • 205
  • 388
0
votes
3 answers

c++ template class member function specialization

I have a problem where I want to specialize a template member function of a template class in the code below. The answer to this question explicit specialization of template class member function seems to suggest that it can't be done. Is that…