What is the meaning of template<> with empty angle brackets in C++?

后端 未结 3 1886
悲哀的现实
悲哀的现实 2020-12-04 19:33
template<>
class A{
//some class data
};

I have seen this kind of code many times. what is the use of template<> in the ab

3条回答
  •  忘掉有多难
    2020-12-04 20:03

    template<> introduces a total specialization of a template. Your example by itself isn't actually valid; you need a more detailed scenario before it becomes useful:

    template 
    class A
    {
        // body for the general case
    };
    
    template <>
    class A
    {
        // body that only applies for T = bool
    };
    
    int main()
    {
        // ...
        A  ai; // uses the first class definition
        A ab; // uses the second class definition
        // ...
    }
    

    It looks strange because it's a special case of a more powerful feature, which is called "partial specialization."

提交回复
热议问题