samedi 1 octobre 2016

Delete[] with a trivially copyable type

/**
 * @brief gk::anydup allows the returning of a type, T, to a duplicate location in memory. Beware!
 * It's an absolutely ugly piece of code for a reason, and that's to get certain types to duplicate okay.
 * @author  Phobos Aryn'dythyrn D'thorga <phobos.gekko@gmail.com>
 *          Christophe <http://ift.tt/2dhovq6;
 *          Kerrek SB <http://ift.tt/2dFqKpw;
 * @date    2016-09
 */
template<typename T>
static T anydup(T *src, size_t len) {
    std::unique_ptr<T[]> ptr(new T[len]);
    std::copy(src, (src + (len * sizeof(T))), ptr.get());
    return *ptr.get();
}

When I execute the above code, I receive a can't delete pointer to incomplete type with regard to the std::unqiue_ptr. So I tried adding a custom deleter, as follows:

struct delUniqueArray {
    void operator()(T *a) const {
        delete[] a;
    }
};

But now there's a SIGABRT when executing the code. What am I doing wrong and how can I fix this?

P.S. I didn't know what title to use for this post, so if possible, please provide suggestions to a better one :)

Aucun commentaire:

Enregistrer un commentaire