In an old C header file, I have the following mapping (32 lines long):
#define IOCON_INDEX_PIO0_17 ( 0)
#define IOCON_INDEX_PIO0_13 ( 1)
#define IOCON_INDEX_PIO0_12 ( 2)
//...
In C++ I could have a function that takes an index and returns an int. To do so I would initialize an array in only one line, but it wouldn't be very much readable.
I would like to replace it with C++ with a modern styling. I have thought to something using lambdas like this:
#include <array>
class test
{
int icon_index(int pio_index)
{
const std::array<int, 32> iocon_index = [](){
std::array<int, 32> buf;
buf[17] = 0;
buf[13] = 1;
buf[12] = 2;
//...
return buf;
}();
return iocon_index[pio_index];
}
};
But when I look to the assembly generated code, it seems to be huge. Also I wonder what RAM it will use.
I have to work with a C++14 compliant compiler, so :
- What is the best manner to replace this legacy C code with C++14?
- What is the best manner to replace this legacy C code with C++17?
constexpr may be a part of the answer...
[edit] I don't want to replace the C code, but adapt it to have a function that takes an int as a parameter and returns another int. It is running on a small embedded system so I want it to be as tight as possible (in terms of flash & RAM).