Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] delete shared_ptr
From: pavel (paul.cpprules_at_[hidden])
Date: 2011-03-17 14:13:04


 Nevin wrote on Thursday, March 17, 2011 at 4:44:15:
> If you are requesting a feature where delete p does the same thing as
> p.reset(), that sounds like a very bad idea to me, because the expected
> semantics of the former (that the object is destroyed) is not what you get
> with the latter (the object is only destroyed when no one owns it). Not to
> mention error-prone as smart pointers should be discouraging people from
> ever explicitly delete-ing objects.

it can be applied to any smart pointer type
shared_ptr is just the first that came to my (excited) mind
i believe that more complete imitation of plain c pointers makes smart
pointers better
even in the case of smart_ptr possibility to use 'delete p;' construct
is a good idea
when you write a line like this

  delete p;

your intention is that you no longer need the data pointed to by 'p'
and it renders more consistent than

  p.reset();

or anything similar

furthermore this can simplify template programming
here is an example

  template<typename ptr_t>
  void takes_ownership(ptr_t p)
  {
    //do something with the data
    delete p;
  }

  takes_ownership(new data_t); //takes ownership

  shared_ptr<data_t> p = new data_t;
  takes_ownership(p); //or does it?

and at last can you please give a couple of reasons why delete'ing
smart pointers is a bad idea?

-- 
Pavel
P.S.
if you notice a grammar mistake or weird phrasing in my message
please point it out

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk