Wednesday, 15 May 2013

templates - C++ error: conflicting return type specified for... templated sub-class -



templates - C++ error: conflicting return type specified for... templated sub-class -

upon first glance, seems correct, code not compile. however, if alter homecoming type of base of operations class double * compile. can explain how/why compiler not see template type "t" valid homecoming type.

base class:

01 template <typename t> 02 class defaultvaluefunctorbase { 03 public: 04 virtual const t operator() (void) const = 0; 05 };

sub-class:

01 class dblptrdft : public defaultvaluefunctorbase<double *> { 02 public: 03 dblptrdft (double default_value_) 04 : defaultvaluefunctorbase<double *>(), 05 _default_value(default_value_), 06 _default_value_ptr(&_default_value) 07 {} 08 09 const double * operator() (void) const { homecoming _default_value_ptr; } 10 11 private: 12 const double *_default_value_ptr; 13 const double _default_value; 14 };

error:

dblptrdft.h:09: error: conflicting homecoming type specified ‘virtual const double* dblptrdft::operator()() const’ defaultvaluefunctorbase.h:04: error: overriding ‘const t defaultvaluefunctorbase::operator()() const [with t = double*]’

the problem const t should read t const, makes clear const qualifier applies t. thus, when replace t double* in template instantiation, homecoming type of base of operations class's phone call operator double* const.

this different homecoming type have in derived class's phone call operator (i.e. const double*, equivalent double const*). in superclass, homecoming constant pointer double. on subclass, homecoming non-constant pointer constant double.

one way prepare application this:

template<typename t> class defaultvaluefunctorbase { public: // not homecoming t const here: top-level const useless anyway virtual t operator() (void) const = 0; }; // instantiate base of operations class template double const* rather double* class dblptrdft : public defaultvaluefunctorbase<double const*> { public: dblptrdft (double default_value_) : defaultvaluefunctorbase<double const*>(), // same alter here of course... _default_value(default_value_), _default_value_ptr(&_default_value) {} // homecoming double const*, consistently instantiated template... double const* operator() (void) const { homecoming _default_value_ptr; } private: double const*_default_value_ptr; double const _default_value; };

c++ templates

No comments:

Post a Comment