vendredi 22 septembre 2017

C unions / views on vectors

It is quite common to use unions (+packing/alignments) in C. When moving to C++, usually we deal with STL vectors that may contain packed data.

What is the clean pattern to access different areas of a vector without using pointers or things like begin()+offset?

I would prefer not to use third party libraries like GSL span.

Imagine something like:

vector<unsigned char> vec(100);

where

data[0..32] is A
data[33..64] is B
etc.

For example, I could do something 'ugly' as mapping a struct pointer to vec.data() but I am looking for an elegant solution.

Aucun commentaire:

Enregistrer un commentaire