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

Class template specialization in class scope?

Why is the specialization S in A legal and S in B not? ( if B is not commented out ) GCC 4.8.1: error: explicit specialization in non-namespace scope ‘class B’ #include #include class Y {}; class X {}; struct A { …
Monotomy
  • 554
  • 1
  • 6
  • 24
12
votes
2 answers

How to spot boxing/unboxing in Scala

Following a suggestion by extempore recently about how to get scala to tell me whether there was boxing going on by looking at the bytecode, I created this class: class X { def foo(ls : Array[Long]) = ls map (_.toDouble) Had a look at the bytecode…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
12
votes
2 answers

Function template specialization with reference to pointer

I have a template function: template void foo(const T& value) { bar(value); x = -1; } I want to specialize it for a set of types: template<> void foo(const char& value) { bar(value); x = 0; } template<> void foo
pingw33n
  • 12,292
  • 2
  • 37
  • 38
10
votes
2 answers

In a template is there a way to write only one specialization for every chrono instantiation? (nanoseconds, milliseconds, seconds, etc)

I have a template that needs to work with the next types: int, float, double, std::chrono::nanoseconds, std::chrono::milliseconds and std::chrono::seconds. The template has a member function to work with int, float and double but I need to write one…
ClK
  • 105
  • 7
10
votes
2 answers

Template specialization for enum values

Is it possible to specialize a class method for individual enum values? Specifically I have an enum and a class as follows: #include #include using namespace std; enum class Animal { dog, cat, bird }; class Sound { …
Vimal
  • 436
  • 1
  • 4
  • 14
10
votes
1 answer

Cannot specialize a Scala method with specializable trait as return type

trait Eq[@specialized -X] { def eq(x: X, y: X): Boolean } trait Keyed[@specialized(Int) X] { def eqOnKey: Eq[X] } The method eqOnKey is not specialized in the generated class Keyed$mcI$sp. How can I specialize this method, i.e. making the…
Tongfei Chen
  • 613
  • 4
  • 14
10
votes
3 answers

@Specializes in Spring

CDI has the feature of Specialization, and I'm looking for that in the Spring world. Details. In CDI, the @Specializes annotation allows one to change the behaviour of a bean just by overriding it. This is completely transparent to users of that…
fxnn
  • 978
  • 6
  • 19
10
votes
6 answers

Specialization of template after instantiation?

My full code is too long, but here is a snippet that will reflect the essence of my problem: class BPCFGParser { public: ... ... class Edge { ... ... }; class ActiveEquivClass { ... ... }; class PassiveEquivClass…
Onur Cobanoglu
  • 211
  • 1
  • 6
  • 9
10
votes
1 answer

C++ Templates - specializing functions

I have the following code: //1 template void c(T in) { cout << "Template c(" << in << ")" << endl; } //2 template<> void c<>(int* in) { cout << "Template specialization b(" << in << ")" <
tomwesolowski
  • 956
  • 1
  • 11
  • 27
10
votes
3 answers

Function specialization in template class for float and double literals

I'm trying to find a solution to have constant numeric literals inside template class method. I'm making some math template classes to be used with float or double types. The problem is that literals are different depending on data type (for example…
umebe
  • 111
  • 1
  • 6
9
votes
5 answers

Inheritance vs Specialization

Considering the following two usage scenarios (exactly as you see them, that is, the end-user will only be interested in using Vector2_t and Vector3_t): [1]Inheritance: template struct VectorBase { }; template
BauerK
9
votes
1 answer

Template argument deduction for inheriting specializations

Consider this code: #include template < typename > struct BB { }; template < > struct BB : BB { }; struct DD : BB { }; template < typename... Args > void ff(BB) { } int…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
9
votes
4 answers

Is partial class template specialization the answer to this design problem?

Say you have a class who's job it is to connect to a remote server. I want to abstract this class to provide two versions, one that connects through UDP and the other through TCP. I want to build the leanest runtime code possible and instead of…
ApplePieIsGood
  • 3,731
  • 6
  • 35
  • 51
9
votes
1 answer

Templates specialization

I have the following set of templates: //1 template< typename T > void funcT( T arg ) { std::cout<<"1: template< typename T > void funcT( T arg )"; } //2 template< typename T > void funcT( T * arg ) { std::cout<<"2: template<…
Dimon
  • 91
  • 1
9
votes
2 answers

Template specialization with a templatized type

I want to specialize a class template with the following function: template class Foo { public: static int bar(); }; The function has no arguments and shall return a result based on the type of Foo. (In this toy example, we return…
kmhofmann
  • 514
  • 6
  • 13
1 2
3
34 35