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
102
votes
3 answers

explicit specialization of template class member function

I need to specialize template member function for some type (let's say double). It works fine while class X itself is not a template class, but when I make it template GCC starts giving compile-time errors. #include #include…
ledokol
  • 1,031
  • 2
  • 8
  • 5
102
votes
6 answers

Template specialization of a single method from a templated class

Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly: template class TClass { public: void doSomething(std::vector * v); }; template
Chuim
  • 1,983
  • 3
  • 17
  • 20
63
votes
4 answers

Default template argument and partial specialization

Please explain to me why the following piece of code complies and works perfectly. I am very confused. #include template class Base {}; template class Base { public: Base() { …
Andy
  • 701
  • 1
  • 7
  • 7
61
votes
5 answers

friend declaration declares a non-template function

I have a base Class akin to the code below. I'm attempting to overload << to use with cout. However, g++ is saying: base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base*)’ declares a non-template…
mike_b
  • 2,127
  • 5
  • 20
  • 25
48
votes
4 answers

Is specialization of std::to_string for custom types allowed by the C++ standard?

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? namespace std { string to_string(::MyClass const & c) { return c.toString(); } } Sample use-case: int main() { MyClass c; std::cout <<…
jotik
  • 17,044
  • 13
  • 58
  • 123
41
votes
5 answers

C++ template specialization without default function

I have the following code that compiles and works well: template T GetGlobal(const char *name); template<> int GetGlobal(const char *name); template<> double GetGlobal(const char *name); However I want to remove the…
anon
40
votes
3 answers

c++ template partial specialization member function

I'm new to templates so maybe this is a trivial thing but I cannot get it to work. I'm trying to get partial specialization of a class member function. The shortest code would be: template class Object{ private: T m_t; …
Simon Righley
  • 4,538
  • 6
  • 26
  • 33
38
votes
3 answers

template class member function only specialization

I am reading the Complete Guide on Templates and it says the following: Where it is talking about class template specialization. Although it is possible to specialize a single member function of a class template, once you have done so, you can no…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
33
votes
6 answers

Selecting a member function using different enable_if conditions

I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this: #include #include template struct Point { void MyFunction(typename…
David Doria
  • 9,873
  • 17
  • 85
  • 147
29
votes
3 answers

C++ partial template specialization in combination with std::is_base_of and std::enable_if

Let's say I have a two classes: Serializable and Printable. So a simple template function which accepts all derived classes of Printable could look like: template
Tim
  • 5,521
  • 8
  • 36
  • 69
28
votes
6 answers

c++ class template specialization, without having to reimplement everything

I have a templatized class like so : template class A { protected: std::vector myVector; public: /* constructors + a bunch of member functions here */ } I would like to add just ONE member function that would…
bob kaggle
  • 431
  • 5
  • 8
27
votes
3 answers

Specializing a template on a lambda in C++0x

I've written a traits class that lets me extract information about the arguments and type of a function or function object in C++0x (tested with gcc 4.5.0). The general case handles function objects: template struct function_traits { …
Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
25
votes
3 answers

How can one provide manually specialized implementations with Scala specialization?

Specialization promises to provide high-efficiency implmentations for primitive types with minimal extra boilerplate. But specialization seems to be too eager for its own good. If I want to specialize a class or method, def foo[@specialized(Byte)…
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
25
votes
5 answers

Template specialization based on inherit class

I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so. -edit- I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of…
user34537
25
votes
2 answers

Template specialization of particular members?

Is it possible to specialize particular members of a template class? Something like: template struct X { void Specialized(); }; template void X::Specialized() { ... } template void…
sold
  • 2,041
  • 5
  • 25
  • 32
1
2 3
34 35