tags:

views:

76

answers:

5

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 <template 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?

A: 

As far as I can tell you did get it right.

Vinzenz
+1  A: 

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.

sbi
A: 

A template specialization actually changes the behaviour of the template for a specific type. eg convert to a string:

template<typename T> std::string convertToString( const T& t )
{
   std::ostringstream oss;
   oss << t;
   return oss.str();
}

Let's specialise that though when our type is already a std::string as it is pointless going through ostringstream

template<> std::string convertToString( const std::string & t )
{
   return t;
}

You can specialise for classes too.

Now instantiation: this is done to allow you to move the compilation for certain types into one compilation unit. This can save you both compilation time and sometimes code-bloat too. Let's say we make the above into a class called StringConvert rather than a function. We will convert a lot of integers to strings so we can instantiate it:

extern template class StringConvert<int>;

One compilation unit must do the same as the above without the extern. Note that the above can also be done (without the extern in the header) with functions that are actually not implemented inline. One of your compilation units will implement them. However then your template is limited only to instantiated types. Sometimes done when the template has a virtual destructor.

CashCow
+5  A: 
Armen Tsirunyan
A: 

An alternative overview

  • Specialization: The class, function or class template member you get when substituting template arguments into the template parameters of a class template or function template.

  • Instantiation: The act of creating a specialization out of a template or class template member. The instantiation can be created out of a partial specializaton, class template member or out of a primary class or function template.

An explicit specialization is one that defines the class, function or member explicitly, without an instantiation.

Johannes Schaub - litb