简体   繁体   中英

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?

这意味着接下来是模板专业化

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

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
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM