lundi 28 mai 2018

Best practice for using "overriden" values for global constants

I got a c++ code in which I got a mapping from string to int using std::map.

Most of the strings got the same values always, for example {"a", 123}. However, there are some cases in which "a" can be some other values, depending on some configuration.

I was starting to save the constants in a map mapping from a string to another map which maps between string and int:

const std::map<std::string, std::map<std::string,int>> myMap 
{
    {"configuration1", {"a", 123}},
    {"configuration1", {"b", 1}},
    {"configuration2", {"a", 124}},
    {"configuration2", {"b", 2}},
    ...
}

This works, but it's very repetitive.

Is there a better way of doing it with the constraints of it needing the map to be global and easily accessible, and also, the configuration itself can change in runtime, meaning it can't be determined before the run starts and just use the same one.

Aucun commentaire:

Enregistrer un commentaire