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
1 answer

How to write specializations of a method of a template class

I'm writing a template class for a dynamic list that allows you to insert three different types of data. I want to create three methods to insert an item within the list using the specializations. What is the right way to do this? template
carlez
  • 93
  • 1
  • 1
  • 5
0
votes
2 answers

Problem with class template specialisations

I'm trying to port some code from VC9 to G++, however Ive run into a problem with template specialisations apparently not being allowed for class members. The following code is an example of these errors for the getValue specialisations of the class…
Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
0
votes
1 answer

When should behaviour that is to be customized in subclasses be put in seperate methods?

consider (python): assume global functions: default_start(), main_behaviour(), default_end(), custom_start(), and custom_end() just as code filler for illustration purposes. class Parent: def on_start_main(self): default_start() def…
Scruffy
  • 908
  • 1
  • 8
  • 21
0
votes
3 answers

Automatic Template Specialization in Template Function Argument

I came up with the following problem (code below): template void printname(const T& t){std::cout< void applyfunc(const T& t, void (*f)(const T& )){(*f)(t);} int main(){ const int a=1; …
0
votes
2 answers

How to avoid redefinition of a class method in a template specialization?

I'm having a problem with a Template specialization since I don't want to redefine a method, I want to use the generic one. template class State { public: State(); ~State(){} HSError_t Update(LoggerData…
Claudiordgz
  • 3,023
  • 1
  • 21
  • 48
0
votes
1 answer

Partial specialization in c++ template

I have a template class to print out the elements in vector. I have both for pointer and reference version. // HEADER class Util { ... template static void print(const std::vector* vectorArray); template static void…
prosseek
  • 182,215
  • 215
  • 566
  • 871
0
votes
1 answer

Template specialization problem

I'm trying really hard to made this work, but I'm having no luck. I'm sure there is a work around, but I haven't run across it yet. Alright, let's see if I can describe the problem and the needs simply enough: I have a RGB template class that can…
0
votes
1 answer

Specializing functions with generic return types

I have what I think is a simple question regarding specializing functions with generic return types. I haven't been able to find another post that answers my question, but perhaps that post exists and I'm just not understanding it (or I haven't…
womple
  • 171
  • 1
  • 3
0
votes
4 answers

Template Specialization or Function Overloading

I know there are other questions like this but they are not very clear. Now I feel as though this is a stupid question because I'm sure I already have the answer but let me ask anyways. So I basically have a function that takes in a string and…
CodingMadeEasy
  • 2,257
  • 4
  • 19
  • 31
0
votes
3 answers

C++ template specialization with not picking up an int

I have the following code: template LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0); } template <> LuaCall& operator>>(int& val) { mResults.push_back(std::make_pair(LUA_RESULT_INTEGER, (void *)&val)); return *this;…
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
0
votes
1 answer

How can I specialize a template that is already specialized

Suppose I have this template: template class Foo I have a partial specialized version of the above template for char: template class Foo How can I further specialize the above template for, say, a =…
Chin
  • 19,717
  • 37
  • 107
  • 164
0
votes
1 answer

Undefined reference to partial specialized template class function during link time

So I had an problem with partial specialization of function templates. I choose the solution described here: Question Now I have this: #include #include template struct helper { static void print(T value) {…
Tim
  • 5,521
  • 8
  • 36
  • 69
0
votes
1 answer

Template specialization, using an other template

Well I have a problem, I don't know how to put it properly, but I have included the code below. I have cut alot out of the code so that it is alot easier to understand the problem, but it might not work 100% now, it's just to illustrate. The problem…
Tim
  • 5,521
  • 8
  • 36
  • 69
0
votes
3 answers

error C2761 member function redeclaration not allowed

I've encountered a problem(error C2761) while writing specializations for a class. My classes are as follows: class Print{ public: typedef class fontA; typedef class fontB; typedef class fontC; typedef class fontD; …
Sebi
  • 4,262
  • 13
  • 60
  • 116
0
votes
2 answers

static member specialization of templated child class and templated base class

I'm trying to have a templated class (here C) that inherits from another templated class (here A) and perform static member specialization (of int var here), but I cant get the right syntax to do so (if it's possible #include…
b3nj1
  • 667
  • 1
  • 6
  • 17
1 2 3
34
35