14

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 :-

#include <iostream>

// Normal template class with a destructor
template <class T> class Test
{
public:
    ~Test() { std::cout << "Normal \n";}
};

// Partial specialization for arrays
template<class T> class Test<T[]>
{
public:
    ~Test() { std::cout << "Array \n"; }
};


int main()
{
    Test<int[3]> i;
}

When I compile this it does not call the specialized version for arrays. If I replace the template with

template<class T> class Test<T[3]>
{
public:
    ~Test() { std::cout << "Array \n"; }
};

Then it does call the specialization but I want this to be called for any array not just ones of a specifed size. Is there any way to write a specialization that gets used for all arrays?

jcoder
  • 29,554
  • 19
  • 87
  • 130
  • I'm not entirely sure, but I think what you've written there is a specialization for arrays of unknown size. That's an incomplete type, but can be used in an `extern` declaration of an array that some other TU will provide. – Steve Jessop Sep 04 '12 at 10:45

1 Answers1

27

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
}
jrok
  • 54,456
  • 9
  • 109
  • 141
  • 2
    This doesn't work for `Test`, though. The non-specialized template will be called. You would need another partial specialization `template class Test` to handle this case. – Remy Lebeau Sep 14 '21 at 20:41