jeudi 20 octobre 2016

std::end for unique_ptr

I want to implement std::end for unique pointer. The problem is that I have to get N(count of elements in array).

1.Approach deduce type from template

template <typename T, size_t N>
T* end(const unique_ptr<T[N]> &arr)
{
    return arr.get() + N;
}

But I got error error: C2893: Failed to specialize function template 'T *test::end(const std::unique_ptr> &)' with [ _Ty=T [N] ] With the following template arguments: 'T=int' 'N=0x00'

It looks like It is not possible to deduce N

2.Get N from allocator. Allocator has to know N to correctly execute delete[]. You could read about this in this article. There are two approaches:

  1. Over-allocate the array and put n just to the left.

  2. Use an associative array with p as the key and n as the value.

The problem is how to get this size cross platform/compiler.

Maybe someone knows better approaches or know how to make this works?

Aucun commentaire:

Enregistrer un commentaire