Home > Back-end >  Can C 20 concept be used to avoid hiding template function from base class?
Can C 20 concept be used to avoid hiding template function from base class?

Time:07-19

I want to use template specialization to query component of a derive object.

The code below works fine.

However, I am new to C .
I am not sure if such compiler behavior is reliable, and want some confirmation.

#include <type_traits>
class B{
   public: template<class T> T* get() requires std::is_same_v< T, B>{ //... [X]
       return this;  
   }
};
class C : virtual public B{
   public: using B::get;
   public: template<class T> T* get() requires std::is_same_v< T, C>{ //... [Y]
       return this;
   }
};
int main(){
   C c;
   B* b1=c.get<B>();
   C* c1=c.get<C>();
   B* b2=c.get<B>();
}

From cppreference , even I use using B::get,

If the derived class already has a member (Y) with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member (X) that is introduced from the base class.

I tried to digest the quote.

Please confirm these 3 statements.

  • [X]&[Y] has the same name AND parameter list.
  • But qualification of [X] and [Y] are difference.
  • Thus, no hides or overrides occur.

Are they all correct?
If so, I will rely on this awesome behavior.

CodePudding user response:

This is specified in namespace.udecl#14, emphasis mine,

When a using-declarator brings declarations from a base class into a derived class, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list ([dcl.fct]), trailing requires-clause (if any), cv-qualification, and ref-qualifier (if any), in a base class (rather than conflicting). Such hidden or overridden declarations are excluded from the set of declarations introduced by the using-declarator.

As to your questions,

  • [X]&[Y] has the same name AND parameter list.
  • But qualification of [X] and [Y] are difference.
  • Thus, no hides or overrides occur.

Yes, [X] and [Y] are exactally the same except for the trailing requires clause. So, no hides occur.

  • Related