I have the following template class. I need to specialize the alloc
function for some specific outT
case.
template <typename inT, typename outT>
class Filter {
public:
typedef inT InputType;
typedef outT OutputType;
struct Options {
int a, b, c;
};
virtual void alloc();
};
// Partial template specialization for SpecificOutputType
template < typename inT>
class Filter<inT, SpecificOutputType> {
virtual void alloc();
};
This results in the Options
class and OutputType
being undefined for gcc, example:
using FilterType = Filter<SomeIn, SpecificOutputType>:
FilterType::Options options;
Results in
error: ‘Options’ is not a member of `Filter<SomeIn, SpecificOutputType>`
This error does not happen if I use some type other than SpecificOutputType
.
How can I solve this?
Every template specialization is independent, they're irrelevant with the primary template, so you have to also define Options
, OutputType
and other necessary members in the template specialization explicitly.
Members of partial specializations are not related to the members of the primary template.
You could make a common base class template to avoid code duplication.
template <typename inT, typename outT>
class FilterBase {
public:
typedef inT InputType;
typedef outT OutputType;
struct Options {
int a, b, c;
};
};
template <typename inT, typename outT>
class Filter : public FilterBase<inT, outT> {
public:
virtual void alloc();
};
// Partial template specialization for SpecificOutputType
template <typename inT>
class Filter<inT, SpecificOutputType> : public FilterBase<inT, SpecificOutputType> {
virtual void alloc();
};