mercredi 23 septembre 2015

Correctly allocate memory in vector

I am trying to implement std::vector as a programming exercise.

Consider the following code snippet:

template <class T, class Allocator = std::allocator<T>>
class vector
{
public:
    using size_type = size_t;
    using allocator_type = Allocator;
    ...
private:
    T* m_data;
    allocator_type m_alloc;
    size_type m_capacity;
    ...
}; 

m_data has type T*. I need to allocate memory using std::allocator_traits<allocator_type>::allocate(m_alloc, m_capacity) which returns std::allocator_traits<allocator_type>::pointer.

Can I assume that pointer can be implicitly cast to T* and assign the value returned from allocate to m_data?

If not, how to correctly allocate memory in vector?

Aucun commentaire:

Enregistrer un commentaire