Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between instantiation and specialization in c++ templates

Tags:

c++

templates

What is the difference between specialization and instantiation in context of C++ templates. From what I have read so far the following is what I have understood about specialization and instantiation.

template <typename T> struct Struct {       T x; };  template<> struct Struct <int> //specialization {      //code };  int main() {    Struct <int> s; //specialized version comes into play    Struct <float> r; // Struct <float> is instantiated by the compiler as shown below  } 

Instantiation of Struct <float> by the compiler

template <typename T=float> struct Struct {     float x; } 

Is my understanding of template instantiation and specialization correct?

like image 341
Saurabh29729 Avatar asked Oct 12 '10 12:10

Saurabh29729


People also ask

What is template specialization used for?

This is called template specialization. Template allows us to define generic classes and generic functions and thus provide support for generic programming. Generic programming is an approach where generic data types are used as parameters in algorithms so that they work for variety of suitable data types.

How do I instantiate a template?

To instantiate a template class explicitly, follow the template keyword by a declaration (not definition) for the class, with the class identifier followed by the template arguments. template class Array<char>; template class String<19>; When you explicitly instantiate a class, all of its members are also instantiated.

Is it necessary to instantiate a template?

In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).

How do you instantiate an object in a template class?

To explicitly instantiate a template class function member, follow the template keyword by a declaration (not definition) for the function, with the function identifier qualified by the template class, followed by the template arguments.


2 Answers

(Implicit) Instantiation

This is what you refer to as instantiation (as mentioned in the Question)

Explicit Instantiation

This is when you tell the compiler to instantiate the template with given types, like this:

template Struct<char>; // used to control the PLACE where the template is inst-ed 

(Explicit) Specialization

This is what you refer to as specialization (as mentioned in the Question)

Partial Specialization

This is when you give an alternative definition to a template for a subset of types, like this:

template<class T> class Struct<T*> {...} // partial specialization for pointers 
like image 100
Armen Tsirunyan Avatar answered Oct 05 '22 22:10

Armen Tsirunyan


What is the difference between specialization and instantiation in context of C++ templates?

Normally (no specializations present) the compiler will create instantiations of a template when they are used, by substituting actual template parameters (int in your example) for the formal template parameters (T) and then compile the resulting code.

If a specialization is present, then for the (set of) special template parameter(s) specified by that specialization, that specialization's implementation is to be used instead of what the compiler would create.

like image 43
sbi Avatar answered Oct 05 '22 22:10

sbi