dimanche 16 juillet 2017

customize std::shared_ptr deleter

In C++ Primer 5th, I've seen something like this:

shared_ptr<T> p(p2,d) p is a copy of the shared_ptr p2 except that p uses that p uses the callable object d in place of delete

But when I test it:

#include <memory>
#include <iostream>

class DebugDelete {
public:
    DebugDelete(std::ostream &o = std::cerr) : os(o) {}
    template <typename T> void operator()(T *p) const
    {
        os << "delete unique_ptr" << std::endl;
        delete p;
    }
private:
    std::ostream &os;
};

int main()
{
    std::shared_ptr<int> sptr, sptr1(sptr, DebugDelete());
}

what I quote seems wrong, sptr1(sptr, DebugDelete()) doesn't work, but sptr1(new int(42), DebugDelete()) works well.

So is it allowed to use a shared_ptr and a deleter to construct a share_ptr like in C++ Primer 5th? Thanks.

Aucun commentaire:

Enregistrer un commentaire