I am trying to create a small interface function and to send it to a JSON formatter class. The JSON works only with (unlimited) key-value pairs, but the value can be (only): string, int, bool
Now I have:
struct Data
{
std::vector<std::pair<std::string, int64_t>> intData;
std::vector<std::pair<std::string, std::string>> strData;
std::vector<std::pair<std::string, bool>> boolData;
};
void func(const Data& data);
But I don't really like it (3 almost similar structures in Data
), perhaps there is a better C++ way.
Ideally for me is to have something (templated) cleaner and to work like:
// pseudocode, I know that it does not work
struct Data
{ // AnyType = string, int or bool
std::vector<std::pair<std::string, AnyType>> data;
};
// or better
using Data = std::vector<std::pair<std::string, AnyType>> data;
Data data;
data.add("int value", 1);
data.add("string value", "str");
data.add("bool value", false);
func(data);
Thanks
LE: I am using C++11, cannot use another standard unfortunately.
Aucun commentaire:
Enregistrer un commentaire