递归模板:对象<auto_ptr<object>>

Recursion template: object<auto_ptr<object>>

本文关键字:gt lt ptr object auto 递归 对象      更新时间:2023-10-16

我希望对象的指针(或句柄)是可自定义的。所以我有时可以选择使用一个指针,它的作用可能类似于ref计数的句柄,但在其他情况下,只使用原始指针。

template <typename PointerType>
class object
{
public:
    // but the PointerType do need the type it point to...
    typename PointerType<object> _parent;
};
int main()
{
    // i choose using shared_ptr as handle
    // object<std::shared_ptr> a;
    // object<std::shared_ptr> b;
    // a._parent = &b;
    // b._parent = nullptr;
    // or a auto_ptr.......
    // object<std::auto_ptr> a;
    // object<std::auto_ptr> b;
    // a._parent = &b;
    // b._parent = nullptr;
}

您需要一个模板模板参数:

template <template <typename...> PointerType>
class object
{
public:
    PointerType<object> _parent;
};

用法类似于:

object<std::shared_ptr> a;
a._parent = std::make_shared<object<std::shared_ptr>>();