Decaying template types
03:36 25 Sep 2026

I have the problem with templates and how to decay a template argument to one of its bases: please consider the following code:

class AbsBase_t {
  virtual int getData() const = 0;
};

class Base_t {
public:
  const int data_{42};
  int getData() const {return data_;}:
};

class Derived_t : public Base_t {
public:
  const int more_data{0};
};

template 
class Functional_Base {
public:
  virtual ~Functional_Base() = default;
  virtual bool eval(const t_type a) const = 0;
};

class Functional : public Functional_Base {
public:
  bool eval(const Base_t a) const override { return a.data_ == 42;};
};

template 
class Host {
public:
  const Functional_Base* fkt_ptr_;
  explicit Host(const Functional_Base* fkt_ptr ) : fkt_ptr_(fkt_ptr) {};

  t_type process(const t_type a) const {
    if (!fkt_ptr_->eval(a))
      throw 1;
    return a;
  };
};

int main() {
  Functional* fkt = new Functional();
  Derived_t d;

  Host h(fkt);

  Derived_t rslt = h.process(d);
};

The compilation will fail with the error :

error: no matching function for call to 'Host::Host(Functional*&)'
...
note: no known conversion for argument 1 from 'Functional*' to 'const Functional_Base*'
   40 |   explicit Host(const Functional_Base* fkt_ptr ) : fkt_ptr_(fkt_ptr) {};

I fully understand the nature of the error any why it comes up. However I cannot see a straight forward solution to make my code compile.

What should happen in my head is that when Host is storing the Functional pointer, it does so tries to decay its template argument, here of class Derived, to the first concrete, aka non-abstract, class in its list of derivatives, which would be Base
Is there a way to make this work?

I also found some solution, where Host gains another template argument, which I however explitly need to state in the initialization of the class, even though it in principle is derivable by decaying the template argument of the first parameter type.

template 
class Host {
public:
  const Functional_Base* fkt_ptr_;
  explicit Host(const Functional_Base* fkt_ptr ) : fkt_ptr_(fkt_ptr) {};

  t_type eval(const t_type a) const {
    if (!fkt_ptr_->eval(a))
      throw 1;
    return a;
  };
};

int main() {
  Functional* fkt = new Functional();
  Derived_t d;

  Host h(fkt);

  Derived_t rslt = h.eval(d);
}

is there some better solution to this?

Thanks in advance

c++ templates