1

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?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
manatttta
  • 3,054
  • 4
  • 34
  • 72
  • 1
    did you mean `FilterType::Options options;` ? – 463035818_is_not_an_ai Oct 10 '16 at 13:53
  • 2
    Template specialisation doesn't copy all the base template + the edits you want ... you have to put in the entire implementation... Alternatively you could have a templated base class, say FilterBase, and then the default Filter has no modification, and FilterSpecialisation only needs to override the function(s) you care about. – UKMonkey Oct 10 '16 at 13:53

1 Answers1

4

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();
};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405