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

Template specialization based on return type of passed lambda - C++

I am trying to make a template specialization based on return type of lambda function which it gets as a parameter. The goal is to have a function to which I can pass a lambda (or another function if possible) let the function measure run time of…
Chariphuk
  • 43
  • 5
4
votes
1 answer

Template Specialization of Function inside of a Templated Class

I have a templated class and inside I have a templated function( different template parameters ) and I having issues getting the compiler to call the correct one. Example: template< class Parm1, class Parm2, class Parm3 > class Class { public: …
Aaron Bahr
4
votes
2 answers

Unrelated specialization must exist to compile?

The following code (which compiles and executes properly, doing what I want) is a minimal example of an oddity I experienced while writing a class to store properties of various types that needed the ability to delete pointers when it no longer…
0x5f3759df
  • 2,349
  • 1
  • 20
  • 25
4
votes
1 answer

C++ {fmt} library, user-defined types with nested replacement fields?

I am trying to add {fmt} into my project, and all is going well, except I hit a little snag when trying to add a user-defined type for my simple Vec2 class. struct Vec2 { float x; float y; }; What I would like is to be able to use the same format…
BeigeAlert
  • 197
  • 1
  • 11
4
votes
3 answers

Specialize template separately for data member and member functions

I would like to specialize a template to do one thing on pointers to data members and another thing on pointers to member functions. This used to work up until gcc 11, with member functions acting as more specific. It still works with clang 11,…
user3188445
  • 4,062
  • 16
  • 26
4
votes
1 answer

template specialization for a const pointer to a const type

I was reading http://bartoszmilewski.wordpress.com/2009/10/21/what-does-haskell-have-to-do-with-c/ and came across this code to check if a type is a pointer or not: template struct isPtr { static const bool value =…
badmaash
  • 4,775
  • 7
  • 46
  • 61
4
votes
1 answer

c++ template specialization method question

I'm new to C++ and I'm trying to use template but I got problems. What I'm trying to do is: try to calculate square of a number using template, and the number may be basic data types like int, float, as well as complex numbers. I also implemented a…
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
4
votes
2 answers

Specializing a template for a container of type T

Given I have a template setup to do something on a type such as... template class SimpleTemplate { private: T m_obj; public: void operator()() { m_obj.DoSomething(); } }; And I want to handle the case where I have a collection of…
4
votes
3 answers

c++ template specialisation method definition

The following code works fine, a simple template class with a definition and a use #include #include using namespace std; template class foo{ public: string what(); }; template string foo::what(){ …
cjh
  • 1,113
  • 1
  • 9
  • 21
4
votes
3 answers

How to specialise the return type of a function with an enum in C++?

I'm using a variant to store a range of types for a syntax parser in C++. Each constituent of a syntax rule has a category (of type enum) and a value. The constituent stores a type of value according to the category. For the sake of example I've…
kiechant
  • 67
  • 5
4
votes
1 answer

Is it legal for class template specialisations to inherit from different base classes?

I have encountered a situation where my class template partial specialisations share a lot of code and it makes sense to move that into a base class. However it does not make sense for all of the specialisations to have the same base class. The…
Fibbs
  • 1,350
  • 1
  • 13
  • 23
4
votes
1 answer

Why is my customed `::swap` function not called?

Here I write a code snippet to see which swap would be called, but the result is neither. Nothing is outputted. #include class Test {}; void swap(const Test&lhs,const Test&rhs) { std::cout << "1"; } namespace std { template<> …
choxsword
  • 3,187
  • 18
  • 44
4
votes
4 answers

Member function overloading/template specialization issue

I've been trying to call the overloaded table::scan_index(std::string, ...) member function without success. For the sake of clarity, I have stripped out all non-relevant code. I have a class called table which has an overloaded/templated member…
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
4
votes
4 answers

Partial Specialization of Operator()

One of my classes declares a templated function: template A do_something(const std::vector &data) which I'd like to partially specialize on typename A. B is a family of types that implement a pretty minimal interface, and we…
Bill Carey
  • 1,395
  • 1
  • 11
  • 20
4
votes
2 answers

Which compiler is correct for the following overloading/specialization behavior?

Consider the following code: #include namespace Foo { template void foo(T *, int) { puts("T"); } template struct foo_fun { static void fun() { foo((T *)0, 0); }; }; } namespace Foo { void…
ididak
  • 5,790
  • 1
  • 20
  • 21