Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concept std::derived_from when the argument is a smart pointer

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?

like image 840
Jane Doe Avatar asked Oct 24 '25 22:10

Jane Doe


1 Answers

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) { }

// Or as an abbreviated function template
bool RegisterModel (std::shared_ptr<std::derived_from<BaseClass> auto> model) { }
like image 146
Artyer Avatar answered Oct 26 '25 13:10

Artyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!