Home > Blockchain >  concept std::derived_from when the argument is a smart pointer
concept std::derived_from when the argument is a smart pointer

Time:05-21

I have a few functions like so

bool RegisterModel (std::shared_ptr<DerivedA> model) { }

bool RegisterModel (std::shared_ptr<DerivedB> model) { }

and i would like to make use of c 20 concepts and implement it like this:

bool RegisterModel (std::derived_from<BaseClass> auto model) { }

This does not work, because i'm passing in shared pointers. It is somehow possible to require a shared pointer that holds an object derived from BaseClass?

CodePudding user response:

Deduce the T from a std::shared_ptr<T> and constrain that:

template<std::derived_from<BaseClass> T>
bool RegisterModel (std::shared_ptr<T> model) { }
  • Related