Assume having MyContainer
template class, like:
template <typename T>
class MyContainer {
public:
T *content;
void *data;
};
Now, assume having MyContetWithData
class as well, which already contains the void *data
field, hence there is no need to duplicate it in MyContainer
, but, all other types yet need said field, only MyContetWithData
does not need it.
How can we specialize MyContainer
so that void *data;
is removed from MyContainer
if T
is of type MyContetWithData
, but keep said field for all other types?
You can simply provide an explicit specialization for MyContetWithData
as shown below:
//primary template for all other types as before
template <typename T>
class MyContainer {
public:
T *content;
void *data;
};
//explicit specialization for MyContetWithData without data
template <> class MyContainer<MyContetWithData>
{
MyContetWithData *content;
};