Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

My compiler says that a member of a base class template is not defined in a derived class template. Why is it not inherited?

0
Posted

My compiler says that a member of a base class template is not defined in a derived class template. Why is it not inherited?

0

template class base { public: void base_func(); }; template class derived : public base { public: void derived_func() { base_func(); // error: base_func not defined } }; A: It is inherited. However, the standard says that unqualified names in a template are generally non-dependent and must be looked up when the template is defined. Since the definition of a dependent base class is not known at that time (there may be specialisations of the base class template that have not yet been seen), unqualified names are never resolved to members of the dependent base class. Where names in the template are supposed to refer to base class members or to indirect base classes, they can either be made dependent by qualifying them or brought into the template’s scope with a using-declaration. In the example, this could be achieved by replacing the call to base_func() with this->base_func() or base::base_func(), or by adding the declaration using base::base_func;.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123