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
5
votes
3 answers

C++11 method template specialization for return type

I've got following class: class Foo { public: template T bar() { cout << "Called with return type: " << typeid(T).name() << endl; T t = //... (some implementation here) return t; } } It's invoked in following…
Daimon
  • 3,703
  • 2
  • 28
  • 30
5
votes
1 answer

Templated function specialization: linker error

I am trying to specialize a function of two template arguments, when template argument types are same. I do it the following way: #include #include using namespace std; template int fun( U& u, T t…
user2052436
  • 4,321
  • 1
  • 25
  • 46
5
votes
4 answers

Using const char** with Template Specialization

I am trying to write a template specialization for a function that returns the maximum value of an array of numeric values (general version) or the longest c-string of an array of c-strings (specialization). If I do not use const-ness, my function…
5
votes
3 answers

Partial template specialization for type

I have a class vec_base defined like so: template class vec_base; and I would like to specialize it so that vec_base and vec_base can have specific…
RamblingMad
  • 5,332
  • 2
  • 24
  • 48
5
votes
3 answers

Help with type traits

Suppose we have the following template class template class Wrap { /* ... */ }; We can not change Wrap. It is important. Let there are classes derived from Wrap. For example, class NewInt : public Wrap { /* ... */ }; class…
5
votes
1 answer

Is this explicit template function specialization of a member template of a member template of a class template valid?

Does anyone know if this explicit specialization is or is not valid: template struct L { template struct O { template static void Fun(U); }; }; template<> template template
5
votes
1 answer

Template function specialization vs. overloading

From some slides about template specialization: #include using namespace std; template X& min(X& a, X& b) { return a > b ? b : a; } int& min(int& a, int & b) { // rewrite of the function in the case of int: cout…
Idan
  • 5,365
  • 5
  • 24
  • 28
5
votes
1 answer

Specializing inner template with default parameters

I'm having trouble specializing an inner template when it's parameters are all known. Here's an example: template < typename T0 > struct outer { template < typename T1 = void, typename T2 = void > struct inner { typedef T1…
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
5
votes
1 answer

Boilerplate-free Scala ArrayBuilder specialization

I'm looking for good practices to avoid rewriting the same code over and over again to achieve unboxedness. Say I have something like this: def speedyArrayMaker[@specialized(Long) A: ClassTag](...): Array[A] = { val builder = Array.newBuilder[A] …
Mysterious Dan
  • 1,316
  • 10
  • 25
5
votes
1 answer

template method specialization inside template class

I need (want) to specialize a method inside a template class, to allow only certain primitive types. (This is not a duplicate question of this) Well i've got this class : template class X { public: template
jav974
  • 1,022
  • 10
  • 22
5
votes
2 answers

Why does this dependent type not count as specialization using the template argument?

I'm trying to group specializations together to avoid writing them multiple times. For example, in the below code, I try to specialize "float" and "double" as one case of implementation for foo::func(); I then use another implementation for…
Jon Watte
  • 6,579
  • 4
  • 53
  • 63
4
votes
3 answers

Same class with 2 or 1 template parameter

How do I make a template specialization that takes 2 parameters versus the normal 1? I was building a pointer class and now I thought about extending to make an array but if I try something like this: template class…
csiz
  • 4,742
  • 9
  • 33
  • 46
4
votes
3 answers

template specialization in C++

I've been trying to understand template specializations. Why is this generating an error (specialization of 'T foo(T, T) [with T = int]' after instantiation) template T foo(T a, T b); int main() { int x=34, y=54; cout<
Zubizaretta
  • 157
  • 1
  • 9
4
votes
3 answers

member template specialization and its scope

It appears to me that C++ does not allow member template specialization in any scope other than namespace and global scope (MS VSC++ Error C3412). But to me it makes sense to specialize a base class's primary member template in the derived class…
Sumant
  • 4,286
  • 1
  • 23
  • 31
4
votes
2 answers

Size of generic type

Is there any way to determine the size in bytes of something like TItem = record Data : T; end; Can I write something like function TItem .GetByteSize : Integer; begin if (T = String) then Result := GetStringByteSize (Data as…
jpfollenius
  • 16,456
  • 10
  • 90
  • 156