Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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) { }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading