5

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?

hongbin
  • 187
  • 1
  • 7

3 Answers3

10

This would mean that what follows is a template specialization.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
2

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
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

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
};
Bo Persson
  • 90,663
  • 31
  • 146
  • 203