概念std :: derived_from当参数是一个明智的指针时

发布于 2025-01-31 12:36:05 字数 363 浏览 3 评论 0原文

我有类似的功能

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

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

,我想利用C ++ 20概念并这样实现:

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

这是不起作用的,因为我传递了共享的指针。有可能需要一个共享指针,该指针容纳baseclass的对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2025-02-07 12:36:05

std :: shared_ptr&lt; t&gt;中推导t>

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

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) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文