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

C++ template specialization: unexpected function overload lookup result

While trying to write a wrapper for shared_ptr that would hide allocation and deallocation of memory from user while supporting inheriting classes, I have stumbled upon very weird errors suggesting that either compiler looks up wrong functions…
3
votes
1 answer

Template specialization restricted by a condition

Is it possible to make compiler choose between template specifications depending on type traits? For example, consider two template implementations of a Compare functional, one for sequential types (strings, vectors, lists, etc.) and another one for…
bkxp
  • 1,115
  • 1
  • 12
  • 20
3
votes
6 answers

Can't get method template to specialize properly

I have a template class that otherwise works perfectly, except I need to overload the insert method in case type T is a string. template class hashtable{ public: void insert(T item){ /* Do Stuff */ }; …
ZachChilders
  • 415
  • 3
  • 15
3
votes
4 answers

How does the compiler know to use a template specialization instead of its own instantiation?

Consider the following files: Foo.H template struct Foo { int foo(); }; template int Foo::foo() { return 6; } Foo.C #include "Foo.H" template <> int Foo::foo() { return 7; } main.C #include…
Luke
  • 65
  • 3
3
votes
2 answers

Defining incomplete struct of specialized class

I'm having problems declaring an incomplete struct inside a class specialization and later defining it. struct Foo { template struct Bar {}; template struct Bar { struct Qux; …
Ambroz Bizjak
  • 7,809
  • 1
  • 38
  • 49
3
votes
4 answers

C++: Extending a template class

I've got the following: template class CVector3 { CVector3 &normalize(); // more stuff }; typedef CVector3 Vector3f; typedef CVector3 Vector3d; I basically want to add a method, toPoint(), that returns a…
digory doo
  • 1,978
  • 2
  • 23
  • 37
3
votes
2 answers

c++ template partial specialization

this might be question that someone asked before but i can't find it... i have a class in a .hpp file : class A{ public: A(){//constructor} ~A(){//destructor} //some public methods and arguments template
PinkFloyd
  • 2,103
  • 4
  • 28
  • 47
3
votes
2 answers

Template specialization of a single method from templated class with multiple template parameters

I'm basically trying to do what was discussed in Template specialization of a single method from a templated class except that my TClass has multiple template Parameters like this: template < class KEY, class TYPE > class TClass { public: : …
marc40000
  • 3,167
  • 9
  • 41
  • 63
3
votes
2 answers

C++11: Specialize/restrict method on depending on a container's value_type

I have a template action method that accepts any kind of STL container C. However the contained items (C::value_type) must be either ClassA or ClassB. So far so good: struct Whatever { template void action(const C& c) { …
syam
  • 14,701
  • 3
  • 41
  • 65
3
votes
1 answer

Partial C++ template specialization in dependent project

Suppose I have a library and multiple projects dependent on that library. The library headers already has some partial class specializations. I want to allow each dependent project to override with its own partial specializations. I need to achieve…
paperjam
  • 8,321
  • 12
  • 53
  • 79
3
votes
2 answers

C++ templates - partial specialisation of member function

I am trying to specialise some geometrical functions depending on 2D or 3D, specified by a template parameter. Best if I include some (very broken) code for a toy version of the problem: template class Point { public: int…
Dave
  • 1,304
  • 1
  • 15
  • 19
3
votes
1 answer

Template specialization and plain old functions

I have just a simple question, check this code please: template < typename A > void foo( A a ) { cout<<"1\n"; }; template< > void foo( float a ) { cout<<"2\n"; } void foo( float a ) { cout<<"3\n"; } int main() { foo( 1.0f…
fjanisze
  • 1,234
  • 11
  • 21
3
votes
4 answers

C++ templated container class: How to best support both ordered and un-ordered item types?

I'm writing a templated C++ generic container class that can optionally maintain its contents in a well-defined order. Previously it used function pointers to order its contents in a sensible type-specific way, but I am attempting to change it to…
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
3
votes
1 answer

Template specialization for unspecialized template arguments

Given I can do this: template struct foo { typedef T type; }; template