Recently I was asked a question. I have the below templated class:
template<size_t SIZE>
class Cache
{
// A lot of class methods omitted
std::array<int, SIZE> _arr;
};
but someone might pass large a size and allocate on the stack, running out of stack memory. So you might suggest changing it to allocate on the heap:
template<size_t SIZE>
class Cache
{
// A lot of class methods omitted
std::unique_ptr<std::array<int, SIZE>> _arr;
};
but now those wanting a small array will pay the cost of indirection (i'm aware this is a very small cost but for the point of the question please accept).
Consequently it was hinted to me template specialization can allow some to choose the small std::array implementation and others to allocate their array on the heap.
I presume this specialization must be at the class level, because the std::array is a class member.
How is this class template specialization achieved without duplicating all the (omitted) class methods?