mercredi 25 janvier 2017

c-style array better container than std::array and std::vector for fixed size data that benifits from move semantics?

I am creating a new implementation of an N-dimensional vector (we'll call it VectorND) to replace some old code in a bigger system. The vector size will be known at compile time, hence c-style arrays, std::array and std::vector are all viable candidates for the underlying data structure of the VectorND class.

I noticed that the vectors get passed around a lot, and think the system would benefit greatly if the new VectorND class could efficiently be moved. (I realize that a lot of the 'passing around' won't be able to use move's, but the vectors occur as rvalue's quite often too)

The std::vector class supports move semantics and as the c-style array is just a pointer both of these allow for trivial a move constructors/assignment operator in the VectorND class. The std::array class however needs O(n) time to be moved, which to me sounds like this "most obvious choice" is in fact the worst in this case.

For the remaining two, the c-style array can be allocated as a single chunk of memory and hence really use the fact that we know the size at compile time.

Am I missing something or is the c-style array indeed the best underlying container for my VectorND class?

(it sounds strange that the newer stl classes would be a worse choice)

EDIT: After reading through my post again, I realize that a std::unique_ptr to an std::array might give me the best of both worlds?

Aucun commentaire:

Enregistrer un commentaire