No-op deallocator for boost::shared_ptr

后端 未结 6 1579
情书的邮戳
情书的邮戳 2020-12-30 02:39

Is there a stock no-op deallocator in Boost to use with boost::shared_ptr for static objects, etc.

I know it\'s ultra-trivial to write, but I don\'t wan

6条回答
  •  忘掉有多难
    2020-12-30 03:12

    FWIW, this what I'm using. I use it in unit tests to adapt a local into a shared_ptr.

    // The class NoOp_sptr_Deleter can be used to construct a shared_ptr<>()
    // that will NOT delete the pointee.
    // This can be helpful in unit-testing. Wrapping a local as a shared_ptr.
    // Do take care with the lifetimes though.
    struct NoOp_sptr_Deleter
    {
        void operator()(void const *) const {}
    };
    
    template
    boost::shared_ptr FakeSharedPtrFromRef(T& aRef)
    {
        return boost::shared_ptr(&aRef, NoOp_sptr_Deleter() );
    }
    

提交回复
热议问题