mercredi 21 mars 2018

Default deleter for raw pointer in unique_ptr

Is it safe to rely on default deleter using std::unique_ptr?

I want to use it like this:

uint8_t* binaryData = new uint8_t[binaryDataSize];
std::unique_ptr<uint8_t> binPtr(binaryData);

So the default deleter in std::unique_ptr looks like this:

template<typename _Up>
typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
operator()(_Up* __ptr) const
{
    static_assert(sizeof(_Tp)>0,
                  "can't delete pointer to incomplete type");
    delete [] __ptr;
}

From my point of view it's safe to use for raw pointers allocated by new[] and not safe with raw pointers allocated by std::malloc. Am i missing something? Is there a better solution?

Aucun commentaire:

Enregistrer un commentaire