I'm developing a system for an embedded application where performance is critical, but I also want well-structured code.
I want to combine arrays (not lists) structured binding declaration but I have some issues.
This is essentially what I want to do.
uint8_t[] read_bytes(uint count) {
// populate bytes by reading from serial connection
// edit, used to be: static uint8_t[count] bytes;
static uint8_t bytes[count];
// ...
return bytes;
}
void handle_instruction() {
auto [ address, data, opcode ] = read_bytes(3);
}
But you can't return an array, you have to pass a pointer to that array, but then you can't unpack (or decompose) that in the handle_instruction function. I think I came to a solution with this
template<uint N>
std::array<uint8_t, N> read_bytes() {
std::array<uint8_t, N> bytes;
// ...
return bytes;
}
void handle_instruction() {
auto [ address, data, opcode ] = read_bytes<3>();
}
Since capacity in a std::array has to be constant, I have to use templates to overcome this. But I feel like this approach is probably more complicated than necessary, is there anything else that could be a better solution to my problem?