vendredi 2 juin 2017

shared_ptr::reset only works with raw pointers?

I have always seen the smart pointer reset function used with raw pointers. When I try to pass another smart pointer to it, it fails. I looked here: http://ift.tt/2ryd6d5 but it does not say much about this. Just want to confirm if this is the case? My code is here: http://ift.tt/2sw7cqo Also here for your convenience:

#include<iostream>
using std::cout; using std::endl;
#include<memory>
using std::shared_ptr;

class Widget
{
public:
  ~Widget()
  {
    cout << "Hi from destructor" << endl;
  }
};

auto wdel = [](Widget* pw)
            {
              cout << "Hi from custom deleter" << endl;
              delete pw;
            };

int main()
{
  {
    shared_ptr<Widget> pw(nullptr,wdel);
    pw.reset(new Widget);
    cout << "Done reset" << endl;
    shared_ptr<Widget> pw2(nullptr,wdel);
    // pw = pw2;   // this works
    pw2.reset(pw); // this does not work
  }
  return 0;
}

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire