samedi 4 avril 2015

code size of using std::array

I'm working on an embedded system (ARM Cortex-M0, so no Linux). I've written a lot of C code for embedded platforms, but this is my first foray into C++.


In my C code, arrays passed into functions always take up 2 parameters. One for the pointer to the data and a second with the length of the array. For example:



void write(uint8_t *buf, size_t bufLen, size_t writeLen);


I'm considering switching these to using std::array (introduced in C++11). It's attractive because it keeps track of its own length but doesn't do any allocation. It looks like the equivalent would be



template<size_t N> void write(array<uint8_t, N> *buf, size_t writeLen);


If my code ends up calling write with 10 differently-sized arrays, does the compiler end up defining 10 different functions? This seems possibly particularly bad if I define a function that takes two (or more) arrays, as the function has to be templated on 2 parameters (the size of each array):



template<size_t N, size_t M>
void readWrite(array<uint8_t, N> *readBuf,
array<uint8_t, M> *writeBuf, size_t writeLen);

Aucun commentaire:

Enregistrer un commentaire