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

Partial specialization advice

Hello all I have this example. Usually in this case I would use Visitor pattern. However for some reason the person who wrote Base, DerivedA, DerivedB prefers dynamic_casts. Keep in mind that I can not change Base, DerivedA, DerivedB classes. I…
rippx009
  • 3
  • 2
0
votes
1 answer

SFINAE using templates, specialization, and achieving type erasure

I've recently come across an interesting implementation of the SFINAE idiom and I'm having trouble working with the specialization classes. Let me explain with a simplified example. I'm utilizing 4 classes in my main function below-- Base, Variant_,…
uclatommy
  • 305
  • 4
  • 11
0
votes
2 answers

Specialize on functor return type

I have the following type signature for a templated member function, which accepts a generic Functor (parameterless) as its sole argument and returns a Container type based on the return-type of the functor: template
0
votes
1 answer

Inheritance from either of two base classes using template specialization and constructor

I am trying to inherit a "derived" class from either base1 or base2. I want to use template specialization for this purpose. I have the following //base1 template class base1 { public: base1(FT m) {} }; //base2 template
0
votes
1 answer

C++ template specialization for member function

I'm trying to implement a very basic Vector3 (Vec3) class. I'm struggling with a special case : Vec3 addition with Vec3. How can I make a template specialization for this case? Any help would be appreciated. Ben #include…
user378147
0
votes
1 answer

Undefined reference to method of partially specialized class

I've been working on a set of template classes that represent various bits of geometry, and I realized that I would like to be able to specialize various classes to handle references and pointers e.g. template class rect{ // as in…
user4578093
  • 231
  • 1
  • 3
  • 10
0
votes
3 answers

Specializations for different types

Can someone tell me how to remove the repeated specializations below? #include #include #include struct Thing { int a, b; void load (std::istream& is) {is >> std::skipws >> a >> b;} }; struct Object { int…
prestokeys
  • 4,817
  • 3
  • 20
  • 43
0
votes
1 answer

stack corrupted when linking to specialized templates

I just went to some really weird behavior and wondered a while what was happening. I wrote a C++ library, containing a class MemoryBlock: So I have two header files, one for the "concept", and one for the partial…
Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
0
votes
1 answer

What should be right behavior when we're trying to instantiate a template?

The code: #include template struct A { int a = A<1>::a; }; int main() { } Is invalid for CLANG, but valid for GCC. What behavior is actually correct? The Standard wasn't pretty clear about that: N4296::14.7.1/1…
user2953119
0
votes
2 answers

How can I specialize a class of a namespace and use it without modifying too much code?

I have a namespace-based code, with a "Model" folder. I call my models statically everywhere in my code: \Myapp\Model\PersonModel::doSomething() Now, I would like to distribute my application in several countries, and be able to override some…
Lideln Kyoku
  • 952
  • 9
  • 20
0
votes
1 answer

c++ partial specialization match reference type

In this code: // decomplexify --------------------------------------------------------------- template struct decomplexify { typedef T type; }; template struct decomplexify > { typedef ELT…
nbecker
  • 1,645
  • 5
  • 17
  • 23
0
votes
2 answers

Template class that wraps a container of type depending on the template parameter

I would like to have a template class that wraps a container, but I would like to make a choice of which container to wrap according to the value of the template parameter. Something like: template class A{ std::vector MyContainer; …
Kae
  • 279
  • 2
  • 10
0
votes
1 answer

How to specialize implementation of a template class if T was derived from a specific base class

I want to specialize implementation of a template class if T was derived from a specific base class. How can I do this? In the code below, x.f() and y.f() should do different work. I want to work not only for 'Derived' but also for all derived…
A.Danesh
  • 844
  • 11
  • 40
0
votes
2 answers

Default template parameter for explicit function specializations

I want to have a set of functions that will accept any uintX_t variant, without having to copy/paste a lot of code. So far I'm trying to use "tagging" to determine what write function to call. For obvious reasons, T = unsigned doesn't work because I…
0
votes
1 answer

Specialization of single template argument

Consider the following code: /* aclass.h */ class AClass { public: template void aMethod(const Vector &); }; /* aclass.inl */ // method for any N template void AClass::aMethod(const…
Shaggi
  • 1,121
  • 1
  • 9
  • 31