In C++ what does template<> mean?

前端 未结 3 1023
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 18:33

It\'s not a syntax I\'m familiar with, but I saw it in another question, an example being:

template<> struct Allowed { };
3条回答
  •  暖寄归人
    2021-01-31 18:59

    It is a template specialization. The typical case would be partial specialization:

    #include 
    
    template struct foo
    {
      void doStuff() { std::cout << "generic foo "; }
    };
    
    template
    struct foo
    {
     void doStuff() { std::cout << "specific foo with T2=int"; }
    };
    

    As you can see, the specialization removes one element from the template parameters and explicitly states a type instead of the removed one. That means if there is only one template type, the <> just become empty:

    template struct bar
    {
      void doStuff() { std::cout << "generic bar"; }
    };
    
    template<>
    struct bar
    {
     void doStuff() { std::cout << "specific bar with T1=int"; }
    };
    

提交回复
热议问题