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

My template specialization differs debug version from release version, is this gcc bug?

First of all, I've got a header file for a class, an specialization declaration without definition(code samples from internet) $ cat foo.h template class foo{ public: static void init(){ return; } }; template<> void…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
4
votes
3 answers

C++ - Add variables and functions to a templated class based on template argument

I have something like this: template class Data { typedef Data data; public: T y; data *ptr[N]; }; And what I would like to able to add variables and functions to the class if N == 2, for…
gmardau
  • 389
  • 2
  • 11
4
votes
1 answer

C++ - specialize class template's member function

I am looking for help with templates. I need to create function in template what will be reacting differently on a specific type. It could looks like this: template class SMTH { void add() {...} // this will be used if specific…
Jara M
  • 125
  • 7
4
votes
0 answers

Template argument deduction in partial specialization

#include using namespace std; template class A{ public: void test() { cout << "normal" << endl;} }; //template class A{ //public: // void test() { cout << "&" << endl;} //}; template
SergeantPenguin
  • 843
  • 7
  • 16
4
votes
1 answer

Can a C++ templated function choose a member variable?

I would like a class to have a function with a template argument, and based on that template argument a particular member variable is manipulated. For example, if function template specialization were allowed, then something like this: struct A { …
user5406764
  • 1,627
  • 2
  • 16
  • 23
4
votes
3 answers

C++ basic template question

I'm slightly confused with template specialization. I have classes Vector2, Vector3 which have operator+= in it (which are defined the following way). Vector2& operator+=(const Vector2& v) { x() += v.x(), y() += v.y(); return…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
4
votes
1 answer

Calling a templated method allowing only subclasses as parameter

Suppose I have set of classes inheriting from a single superclass S: class S{ ... }; class C1 : public S{ ... }; class C2 : public S{ ... }; Then suppose I have a templated method: template void foo(T* instance); I would like to…
Pierluigi
  • 2,212
  • 1
  • 25
  • 39
4
votes
3 answers

How can a C++ template be specialized for all 32-bit POD types?

I've developed a simple template function for swapping the byte order of a single field: template inline void SwapEndian(T& ptr) { char *bytes = reinterpret_cast(&ptr); int a = sizeof(T) / 2; while (a--) { …
4
votes
1 answer

Converting with yaml-cpp to a template class

I have my own container: template class MyContainer {} And I'm using yaml-cpp for loading some data into this container. So I need to write specialization for convert struct: template<> struct convert< MyContainer > {}; template<>…
Nick Roz
  • 3,918
  • 2
  • 36
  • 57
4
votes
2 answers

std::basic_string full specialization (g++ conflict)

I am trying to define a full specialization of std::basic_string< char, char_traits, allocator > which is typedef'd (in g++) by the header. The problem is, if I include first, g++ sees the typedef as an instantiation of…
SoapBox
  • 20,457
  • 3
  • 51
  • 87
4
votes
3 answers

Does this mimic perfectly a function template specialization?

Since the function template in the following code is a member of a class template, it can't be specialized without specializing the enclosing class. But if the compiler's full optimizations are on (assume Visual Studio 2010), will the…
zeroes00
  • 531
  • 3
  • 16
4
votes
4 answers

c++ recursive template specialisation

I wrote an abstract container template class that should define numeric operators (unary + and -, binary +, - and *) if it make sens for the template parameter (that is, if it is a numeric type). Then, I would like to apply those numeric operations…
Titou
  • 63
  • 5
4
votes
2 answers

C++: How to partial specialization a template function in a template class

Code speaks: template struct Vector3D { Group x, y, z; Vector3D(Group x, Group y, Group z) : x(x), y(y), z(z) { } template Group Norm() const; }; template template Group…
buaagg
  • 599
  • 6
  • 11
4
votes
4 answers

Specializing a template by a template base class

I'm writing a template for which I'm trying to provide a specialization on a class which itself is a template class. When using it I'm actually instanciating it with derivitives of the templated class, so I have something like…
Grizzly
  • 19,595
  • 4
  • 60
  • 78
4
votes
2 answers

specialization on const member function pointers

I am trying to specialize some utility code on const member functions, but have problems to get a simple test-case to work. To simplify the work i am utilizing Boost.FunctionTypes and its components template - a MPL sequence which…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236