包含它的模板化类型的shared_ptr

shared_ptr of a templated type enclosing it

本文关键字:shared ptr 类型 包含它      更新时间:2023-10-16

是否可以让shared_ptr指向包含它的模板化类型?例如

pair<shared_ptr<ThisPairType>, int>

应该如何定义?

无法直接编写所需的内容,因为类型的名称在您想要使用它时尚未完成。您甚至无法用typedef来解决这个问题,因为无法转发声明 typedef。

我建议简单地编写一个结构:

struct Recursive { std::shared_ptr<Recursive> first; int second; };

或者按照 igor 的评论建议从pair继承:

我能想到的最好的是struct ThisPairType : std::pair<std::shared_ptr<ThisPairType>, int> {};这不是你想要的,但很接近。