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

Partial specialization with type nested in a templated class

I'm playing with templates and partial specialization, but there is one specialization I don't know how to write... I'll simplify code to make it easier to read. Let's condiser template class x { ... }; Usually, I can…
neodelphi
  • 2,706
  • 1
  • 15
  • 22
6
votes
6 answers

Unrolling loops using templates in C++ with partial specialization

I'm trying to use templates to unroll a loop in C++ as follows. #include template< class T, T i > struct printDown { static void run(void) { std::cout << i << "\n"; printDown< T, i - 1 >::run(); } }; template<…
6
votes
5 answers

How can I specialize a typedef and its implicit type differently?

I have something like this: typedef int AnotherType; template Func( T Value ); // And I want to specialize these two cases separately: template <> bool Func( int Value ) {...} template <> bool Func( AnotherType Value…
Frigg
  • 369
  • 1
  • 3
  • 12
6
votes
2 answers

specialize only (a part of) one method of a template class

If I have a template class template class C { public: void method1() { ... } void method2() { ... } std::string method3(T &t) { // ... std::string s = t.SerializeToString(); // ... return s; …
fferri
  • 18,285
  • 5
  • 46
  • 95
6
votes
3 answers

Ambiguous partial template specialization

I've got a trait class which I need to specialize (and partial-specialize) many times. Some partial specializations overlap: template< typename T > struct C { }; template< typename T1, typename T2 > struct TRAIT { }; template< typename T > struct…
peoro
  • 25,562
  • 20
  • 98
  • 150
6
votes
1 answer

Wrong specialized generic function gets called in Swift 3 from an indirect call

I have code that follows the general design of: protocol DispatchType {} class DispatchType1: DispatchType {} class DispatchType2: DispatchType {} func doBar(value:D) { print("general function called") } func…
Abe Schneider
  • 977
  • 1
  • 11
  • 23
6
votes
3 answers

How to subclass requests in python through inheritance

I would like to specialize / subclass the requests package to add some method with custom functionality. I tried to do this: # concrete_requests.py import requests class concreteRequests(requests): def __init__(self): …
Jeflopo
  • 2,192
  • 4
  • 34
  • 46
6
votes
2 answers

C++: template to check if expression compiles

When writing template specialization with SFINAE you often come to the point where you need to write a whole new specialization because of one small not-existing member or function. I would like to pack this selection into a small statement like…
tly
  • 1,202
  • 14
  • 17
6
votes
3 answers

C++ template specialization/overloading

First of all, I'm sorry for the vague title of this question. I wasn't sure how to summarize it. The thing I want to achieve is the following, I want to be able to pass template non-type parameters of different types to the same class-template,…
JorenHeit
  • 3,877
  • 2
  • 22
  • 28
6
votes
4 answers

Overriding multiple inherited templated functions with specialized versions

Okay, sample code first; this is my attempt at communicating what it is that I'm trying to do, although it doesn't compile: #include template class Base { public: virtual void my_callback() = 0; }; class Derived1 :…
Narfanator
  • 5,595
  • 3
  • 39
  • 71
6
votes
2 answers

Using valid STATIC member function of class that can't be installed

I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel compiler, but fails under MSVC9. MSVC tells "use of undefined type c_traits, while compiling class template member function void foo::go(void) with…
Artyom
  • 31,019
  • 21
  • 127
  • 215
6
votes
1 answer

Defining an Inner class member function template with a (non type) enum argument

I'm having difficulty defining and specializing a member function update() of an inner class Outer::Inner that is templated on a non-type (enum) argument. #include template struct Outer { struct Inner { …
Olumide
  • 5,397
  • 10
  • 55
  • 104
6
votes
2 answers

C++ - Use default template as base for specialization

I want to write a math vector template. I have a class which accepts type and size as template argument, with a lot of math operation methods. Now I want to write specializations where Vector<3> for instance has x, y, z as members which refer to…
weltensturm
  • 2,164
  • 16
  • 17
6
votes
2 answers

How to avoid repeating the whole class template when only one member specialization is needed

Suppose you have: template class A { template void foo(const T1& t1) {} // // Lots of other definitions (all templated) // }; and you would like to specialize foo(const T1&) but only for a specialized A. Like…
ritter
  • 7,447
  • 7
  • 51
  • 84
6
votes
6 answers

Template class inside class template in c++

noob here still experimenting with templates. Trying to write a message processing class template template class MessageProcessor { //constructor, destructor defined //Code using t_ and other functions foo( void ) { //More code…
user106740
  • 107
  • 1
  • 1
  • 8