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

Usage of @specialized in traits

I have a trait and an implementation looking like: trait Foo[A] { def bar[B >: A: Ordering]: Foo[B] } class FooImpl[A]( val a: A, val values: List[Foo[A]] ) extends Foo[A] { def bar[B >: A] = { /* concrete implementation */} } I would like to…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
5
votes
1 answer

Specialization of template function after point of use will break the compilation

Consider next example : #include template< int a > void foo(); int main(int argn, char* argv[]) { foo<1>(); } template<> void foo<1>() { std::cout<<1<
BЈовић
  • 62,405
  • 41
  • 173
  • 273
5
votes
2 answers

Explicit instantiation of function template specialization

I am trying to create a global function template specialized for some given types. It looks something like that: A.h (primary template, template specialization, extern) template void foo() { std::cout << "default stuff" << std::endl;…
Touloudou
  • 2,079
  • 1
  • 17
  • 28
5
votes
5 answers

overloaded member functions for a particular template specialisation

I have a class tPoint that would be implemented having different base types so templateclass tPoint{ T x; T y; public: void Set(T ix, T iy){x=ix;y=iy;} }; When the type T is int, tPoint, I want a special Set(float, float)…
tru7
  • 6,348
  • 5
  • 35
  • 59
5
votes
2 answers

Why doesn't specialization of std::hash() need to be injected into std namespace in this particular case?

Consider using namespace std; template struct hash> { inline size_t operator()(const pair &v) const { return 0; } }; In this case both GCC and Clang compile it fine with no warnings.…
1110101001
  • 4,662
  • 7
  • 26
  • 48
5
votes
1 answer

Failed to specialize function template

This is homework, although it's already submitted with a different approach. I'm getting the following from Visual Studio 2008 error C2893: Failed to specialize function template 'void std::sort(_RanIt,_RanIt,_Pr)' The code is as…
5
votes
1 answer

What is the Type This struct is Inheriting From?

So this example from: http://en.cppreference.com/w/cpp/utility/variant/visit declares the specialized type: template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...) ->…
5
votes
3 answers

Multiple types in one specialized D template

Say I have to deal ushort and uint some way, but string differently. So guess I need one specialized template for string and other to both ushort and uint. Is it? // for most void func(T)(T var) { ... } // for uint and ushort void func(T: uint,…
Pedro Lacerda
  • 1,098
  • 9
  • 23
5
votes
2 answers

Is it possible to specialize on a static lifetime?

I want to specialize &'static str from &'a str: use std::borrow::Cow; struct MyString { inner: Cow<'static, str>, } impl From<&'static str> for MyString { fn from(x: &'static str) -> Self { MyString { inner:…
Craig M. Brandenburg
  • 3,354
  • 5
  • 25
  • 37
5
votes
1 answer

C++ - variadic template partial specialization

Let's have a simple snippet: template struct A { void operator()() { std::cout << "A"; } }; template struct A { void operator()() { std::cout << "B"; } }; template
Radek
  • 518
  • 5
  • 12
5
votes
1 answer

How to implement specialized versions of a generic function?

I'd like to have multiple versions of a function optimized for type of its arguments, and Rust call appropriate one depending on context. In my case all arguments have the same type, and all are equivalent, so it'd rather avoid having a self…
Kornel
  • 97,764
  • 37
  • 219
  • 309
5
votes
3 answers

Using a Class Template's Internal Types from a Specialized Class

EDIT: I didn't actually get a chance to test out any of the suggested solutions as I went on a vacation, and by the time I was back, the people responsible for the class template had made some changes that allowed me to get around the need to use…
Argent
  • 795
  • 2
  • 7
  • 25
5
votes
3 answers

Scala specialization for numeric operation of primitive types

I wrote a function doing simple math: def clamp(num: Double, min: Double, max: Double) = if (num < min) min else if (num > max) max else num It is very simple, until I needed the same function with Long type. I generalized it with type parameter…
pocorall
  • 1,247
  • 1
  • 10
  • 24
5
votes
1 answer

C++ - Function template specialization not being called

I have the following code: template bool validate(const T& minimum, const T& maximum, const T& testValue) { return testValue >= minimum && testValue <= maximum; } template<> bool validate(const char& minimum, const…
Matt Welke
  • 1,441
  • 1
  • 15
  • 40
5
votes
1 answer

No generated code for explicitly specialized template even with explicit instantiation

I'm getting consistent behavior from both gcc 4.8.3 and clang 3.2, but do not understand why it is happening. Despite the fact that I have an explicit instantiation for a class template, the code is not being generated and I get an undefined symbol…
Jay
  • 75
  • 5