No-op deallocator for boost::shared_ptr

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

    Yes there is one here:

    #include  // for null_deleter
    
    class Foo
    {
      int x;
    };
    
    Foo foo;
    boost::shared_ptr< Foo > sharedfoo( &foo, boost::serialization::null_deleter() );
    

    There is, of course, a danger with the fact that you need to know the function you call doesn't store the shared_ptr for later use, as it actually goes against the policy of shared_ptr in that the underlying object remains valid until that of the last instance of the shared_ptr.

提交回复
热议问题