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

Partial template specialization based on "signed-ness" of integer type?

Given: template inline bool f( T n ) { return n >= 0 && n <= 100; } When used with an unsigned type generates a warning: unsigned n; f( n ); // warning: comparison n >= 0 is always true Is there any clever way not to do the…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
21
votes
3 answers

Template Specialization for a function without Parameters

I need to specialize a function template in c++. template void doStuff() {} To template<> void doStuff(); and template<> void doStuff(); I guess that is not the correct syntax (since it is not…
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
19
votes
3 answers

static member initialization for specialized template class

class A { }; template class B { public: static int a[S]; B() { a[0] = 0; } }; template<> int B::a[1]; int main() { B t; t; } It compiles under GCC…
kaspy
  • 191
  • 1
  • 1
  • 3
19
votes
5 answers

Template specialization on template member of template class

This is probably only a syntax problem. So i have this template class : template class Allocator> class basic_data_object { template using array_container = std::vector>; }; And…
Drax
  • 12,682
  • 7
  • 45
  • 85
18
votes
7 answers

C++ template specialization of constructor

I have a templated class A and two typedefs A and A. How do I override the constructor for A ? The following does not work: template class A; typedef A
user231536
  • 2,661
  • 4
  • 33
  • 45
16
votes
3 answers

Is making a function template specialization virtual legal?

In C++, a function template specialization is supposed to act exactly like a normal function. Does that mean that I can make one virtual? For example: struct A { template void f(); template <> virtual void f() {} }; struct B…
alexk7
  • 2,721
  • 1
  • 21
  • 18
16
votes
4 answers

C++ single template specialisation with multiple template parameters

Hallo! I would like to specialise only one of two template types. E.g. template class X should have a special implementation for a single function X::someFunc(). Sample code: main.h: #include…
tauran
  • 7,986
  • 6
  • 41
  • 48
16
votes
1 answer

Why are function template specializations not allowed inside a class?

After having found answers to many of my questions on stackoverflow, I have now come up against a question of which I can't find the answer and I hope that someone is willing to help me! My problem is that I want to do an explicit templatization of…
Malin
  • 771
  • 1
  • 7
  • 16
16
votes
7 answers

How to prevent specialization of std::vector

I have a templated class that has a data member of type std::vector, where T is also a parameter of my templated class. In my template class I have quite some logic that does this: T &value = m_vector[index]; This doesn't seem to compile when T…
Patrick
  • 23,217
  • 12
  • 67
  • 130
15
votes
5 answers

C++ template class specialization: why do common methods need to be re-implemented

In the sample: #include using namespace std; class B { public: virtual void pvf() = 0; }; template class D : public B { public: D(){} virtual void pvf() {} private: string data; }; template <> class D
Jaime
  • 1,182
  • 2
  • 12
  • 29
15
votes
6 answers

One template specialization for multiple classes

Let's assume we have a template function "foo": template void foo(T arg) { ... } I can make specialization for some particular type, e.g. template<> void foo(int arg) { ... } If I wanted to use the same specialization for all builtin…
peper0
  • 3,111
  • 23
  • 35
14
votes
7 answers

Is there a way to use template specialization to separate new from new[]?

I have an auto pointer class and in the constructor I am passing in a pointer. I want to be able to separate new from new[] in the constructor so that I can properly call delete or delete[] in the destructor. Can this be done through template…
Marlon
  • 19,924
  • 12
  • 70
  • 101
13
votes
1 answer

Partial template specialization ambiguity

I cant see why the statement in main is ambiguous. template struct X { void f() { cout << "Primary template" << endl; } }; template struct X {void f() { cout << "Partial specialization 1" <<…
13
votes
1 answer

C++ linking and template specializations

I'm studying the behavior of the C++ linker with respect to template specializations. I'm using Microsoft Visual C++ 2010 for these experiments. I don't know if the behavior is the same with other toolchains (e.g. gcc). Here's a first code…
François Beaune
  • 4,270
  • 7
  • 41
  • 65
13
votes
3 answers

Specializing function template for reference types

Why is the output of this code : #include template void f(T param) { std::cout << "General" << std::endl ; } template<> void f(int& param) { std::cout << "int&" << std::endl ; } int main() { float x ; …
1
2
3
34 35