Difference between instantiation and specialization in c++ templates

Viewed 15760

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?

6 Answers

In c++ 11.

instantiation:

Instantiate the template with given template arguments

template <typename T>
struct test{ T m; };

template test<int>;//explicit instantiation

which result in a definition of a struct with a identifier test<int>

test<int> a;//implicit instantiation

if template <typename T> struct test has been instantiated with argument T = int before(explicit or implicit), then it's just a struct instantiation. Otherwise it will instantiate template <typename T> struct test with argument T = int first implicitly and then instantiate an instance of struct test<int>

specialization:

a specialization is still a template, you still need instantiation to get the real code.

template <typename T>
struct test{ T m; };
template <> struct test<int>{ int newM; } //specialization

The most useful of template specialization is probably that you can create different templates for different template arguments which means you can have different definitions of class or function for different template arguments.

template<> struct test<char>{ int cm; }//specialization for char
test<char> a;
a.cm = 1;

template<> struct test<long> { int lm; }//specialization for long
test<long> a;
a.lm = 1;

In addition to these full template specializations above, there(only class template) exits partial template specialization also.

template<typename T>
struct test {};
template <typename T> struct test<const T>{};//partial specialization for const T


template <typename A, typename B>
struct test {};
template <typename B> struct test<int, B>{};//partial specialization for A = int

A specialized template is no longer just a template. Instead, it is either an actual class or an actual function.

A specialization is from either an instantiation or an explicit specialization, cf 14.7.4 below.

An instantiation is based on a primary template definition. A sample implicit class template instantiation,

template<typename T>
class foo {}

foo<int> foo_int_object;

A sample explicit class template instantiation,

template class foo<double>;

An explicit specialization has a different definition from it's primary template.

template<>
class foo<bool> {}

// extract from standard

14 Templates

14.7 Template instantiation and specialization

4 An instantiated template specialization can be either implicitly instantiated (14.7.1) for a given argument list or be explicitly instantiated (14.7.2). A specialization is a class, function, or class member that is either instantiated or explicitly specialized (14.7.3).

Related