dimanche 16 juin 2019

Why is vector of unique_ptr the prefered way to store pointers?

The common approche to make a vector of pointer, of int for example, is vector<unique_pointer<int>>.

IIUC, each time we access an element will call unique_ptr::get(). There is also a little overhead.

Why isn't vector of pointer with "custom deleter", if such a thing exists (I don't have used allocators), more standard ? That is, a smart vector instead of a vector of smart pointer. It will eliminate the little overhead of using unique_ptr::get().

Something like vector<int*, delete_on_destroy_allocator<int>> or unique_vector<int>.

The vector would take the behaviour "delete pointer when destroy" instead of duplicate this behaviour in each unique_ptr.

Aucun commentaire:

Enregistrer un commentaire