No-op deallocator for boost::shared_ptr

后端 未结 6 1540
情书的邮戳
情书的邮戳 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:10

    Solution uses Boost.Lambda:

    #include 
    #include 
    
    int main()
    {
        int *p = new int(5);
    
        {
            boost::shared_ptr sp(p, boost::lambda::_1);
        }
    
        delete p;
    }
    

    'boost::lambda::_1' creates an empty functor that takes one argument.

    You'll probably want to put a //comment in there to let people know why you did it, though.

提交回复
热议问题