hongbin
hongbin

Reputation: 187

what does template<> (without any class T in the <>) mean?

I'm reading some source code in stl_construct.h, In most cases it has sth in the <> and i see some lines with only "template<> ...". what's this?

Upvotes: 5

Views: 204

Answers (3)

Alok Save
Alok Save

Reputation: 206518

Guess, I completely misread the Q and answered something that was not being asked.
So here I answer the Q being asked:

It is an Explicit Specialization with an empty template argument list.

When you instantiate a template with a given set of template arguments the compiler generates a new definition based on those template arguments. But there is a facility to override this behavior of definition generation. Instead of compiler generating the definition We can specify the definition the compiler should use for a given set of template arguments. This is called explicit specialization.

The template<> prefix indicates that the following template declaration takes no template parameters.

Explicit specialization can be applied to:

  • Function or class template
  • Member function of a class template
  • Static data member of a class template
  • Member class of a class template
  • Member function template of a class template &
  • Member class template of a class template

Upvotes: 2

Bo Persson
Bo Persson

Reputation: 92241

It's a template specialization where all template parameters are fully specified, and there happens to be no parameters left in the <>.

For example:

template<class A, class B>   // base template
struct Something
{ 
    // do something here
};

template<class A>            // specialize for B = int
struct Something<A, int>
{ 
    // do something different here
};

template<>                   // specialize both parameters
struct Something<double, int>
{ 
    // do something here too
};

Upvotes: 0

dirkgently
dirkgently

Reputation: 111130

This would mean that what follows is a template specialization.

Upvotes: 10

Related Questions