I am trying to convert a long list of key and value pairs into a map so that I can just iterate over it instead of defining each of them line-by-line, but I have a mix of data types for the values (uint8_t, uint16_t, uint32_t, and uint64_t).
Here's a sample of the data I am trying to build a mapping of:
static constexpr uint8_t data1{0x01};
static constexpr uint16_t data2{0x0002};
static constexpr uint32_t data3{0x00000003};
static constexpr uint64_t data4{0x0000000000000004};
I tried doing a union and a struct:
union/struct dtypes {
uint8_t i;
uint16_t j;
uint32_t k;
uint64_t l;
};
Then here's my attempt at creating the map:
std::map<std::string, dtypes> mymap = {{"data1", 0x01},
{"data2", 0x0002},
{"data3", 0x00000003},
{"data4", 0x0000000000000004}};
Then I try to do a for loop to populate a vector of uint8_t's (call it vec):
for (auto iter = mymap.begin(); iter != mymap.end(); ++iter) {
byte_pushback(vec, iter->second)
};
But the error message I am getting is:
error: could not convert "'{{"data1", 0}, {"data2", 2}, and so on..' from '<brace-enclosed initializer list>' to 'std::map<std::__cxx11::basis_string<char>, dtypes>'
{"data4", 0x0000000000000004}};
^
|
<brace-enclosed initializer list>
How could I do this properly?
This byte_pushback() function breaks the uint16_t, uint32_t, uint64_t into a bunch of uint8_ts and does a push back, so data2 would be {0x00, 0x02}.