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

Specializing a function for a private class?

Is there any way to specialize a function (say, std::swap) for a private class? For example, when I test this: #include class Outer { struct Inner { int a; void swap(Inner &other) { using…
user541686
  • 205,094
  • 128
  • 528
  • 886
4
votes
2 answers

Template class inheritance from a different specialization

This is a question out of curiosity on C++ rules without any real practical usage. When toying around with templates, I created a class hierarchy like so: #include // Declaration template struct A; // Specialization for X =…
Zeenobit
  • 4,954
  • 3
  • 34
  • 46
4
votes
2 answers

Function template explicit specialization c++

My book mentions two ways for explicit specialization: template <> void Swap (int &, int &); template <> void Swap(int &, int&); what is the difference between both? when to use one and when to use the other? what is exactly the <> after the…
jazzybazz
  • 1,807
  • 4
  • 17
  • 21
4
votes
1 answer

Inlining causes specialized member function of template class overriding virtual functions to get overlooked

I wanted to share a strange example with you guys that I stumbled upon and that kept me thinking for two days. For this example to work you need: triangle-shaped virtual inheritance (on member function getAsString()) member function specialization…
4
votes
2 answers

Template partial specialization problems

I am trying to write a size and type generic vector class for math programming. I am having problems with partial specialization. The problem occurs when I try to specialize a member method of my vector class for given size. I can provide a simple…
F.L.
  • 559
  • 4
  • 11
4
votes
3 answers

Function template specialization with a template class

Possible Duplicate: partial specialization of function template I can't find anywhere a solution for my problem, because if I search with the keywords I come up with would give me solutions suited for different problems. I understand that this…
Tim
  • 5,521
  • 8
  • 36
  • 69
3
votes
1 answer

C++ specialize a template class to take an additional template parameter

Is it possible to specialize a template class to take additional template parameters ? For example: template struct X { void foo() { cerr << "Generic" << endl;} }; template <> template struct X { void foo() { cerr <<…
ATemp
  • 319
  • 3
  • 10
3
votes
2 answers

Specialize a template with a template

I have a (free) function template that looks like this template T get(); I now want to specialize this function for a class, which itself is a template. But my compiler doesn't want to compile it, and I'm asking now if that is even…
cooky451
  • 3,460
  • 1
  • 21
  • 39
3
votes
3 answers

C#: Generics, Polymorphism And Specialization

I am trying to use generics with specialization. See the code below. What I want to do is make runtime engine understand that specialization of the function is available based on type and it should use that instead of generic method. Is it possible…
neel roy
  • 147
  • 1
  • 13
3
votes
1 answer

vb.net specilized/overloaded generics

I tend to loath repetition in code, so when I come across a problem where the only different is types I tend to use generics. Coming from a C++ background I find vb.net's version to be rather frustrating, I know C++ has template specialization and I…
Apeiron
  • 694
  • 7
  • 13
3
votes
0 answers

Convincing the compiler that a GAT has the same type as another GAT

I was playing with some code, trying to see how wild I can get. The basic idea came to me when implementing merge sort with a custom comparator. We need to pass down the comparator (impl FnMut), but we cannot pass it owned because we need to pass it…
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
3
votes
1 answer

C++ template function specialization error when specialization is in different header

In cnvt.h header there is: template std::optional cnvt(t_from); And in header int.h, we have: #include "cnvt.h" template <> std::optional cnvt(const char *) { // actual code…
canellas
  • 491
  • 5
  • 17
3
votes
1 answer

How to check type equality between non-template class and template class?

Suppose I have class A {}; template class Wrapper {}; I'm trying to check if the wrapper's first inner type is A. typeCheck(new A(), new Wrapper); // expect true typeCheck(new A(), new Wrapper); // expect…
LunaticJape
  • 1,446
  • 4
  • 21
  • 39
3
votes
1 answer

c++ Friend function not recognised as friend with template specialisation

I am trying to declare a function as friend to a class template with a protected member. Below is a minimal example. template class myClass{ public: friend void f(const myClass& c); protected: int a; }; If I now define the function…
marco
  • 167
  • 6
3
votes
1 answer

Can I make separate definitions of function template members of a class template?

Here's a minimal code example to show what I'm attempting that works, but not how I'd like it to: #include #include #include struct string_tag { using R=const std::string; }; struct int_tag { using R=const…