Partial template specialization for arrays

后端 未结 1 1144
孤街浪徒
孤街浪徒 2020-12-28 21:34

Seached for this, but can\'t find a similar question. If there is one please close this question. This isn\'t my real code, just an example to demonstrate :-



        
相关标签:
1条回答
  • 2020-12-28 22:02

    Capture the size with an additional non-type parameter:

    #include <iostream>
    
    template <class T> class Test
    {
    public:
        ~Test() { std::cout << "Normal \n";}
    };
    
    template<class T, size_t N> class Test<T[N]>
    {
    public:
        ~Test() { std::cout << "Array " << N << '\n'; }
    };
    
    int main()
    {
        Test<int[3]> i;  // Array 3
        Test<int[5]> j;  // Array 5
    }
    
    0 讨论(0)
提交回复
热议问题