Just stumbled across the settings parser on the wiki, and I have a question regarding the data structure you save the data in. Why do you use a std:vector<std::pair<std::string, std::string>> ?
With a vector of pairs you can enter duplicate settings, and your code then will always only select the value with the lower index-key in your vector. The second value with the same key (but higher index in your vector) will never get selected (and shouldn't be there in the first place in my opinion).
That's a little design problem and I think this should be addressed. If a std::map<std::string, std::string> is used instead of a vector, only one instance of a key can live inside the map, which gets overwritten if the same key is present in the text file (which could also be eliminated easily using a map).
Also maybe some checking if the given text file yields a valid format maybe benefical, if the user manually edits the text-file (which developers often do for unit testing). Some points from the top of my head:
- Left- and right-trimming the lines for whitespace before parsing them
- converting the key to be always lower (or upper) case, so that it's really unique (so case-variants of identical keys can also be detected)
Throwing an exception instead of returning false if the text file cannot be read may also be better, a bool-return can be ignored, a thrown exception not
Just some improvement thoughts to make the already good implementation more robust.
Cheers!
OT-Question:
I agree that it's useless as part of the interface, but it can sometimes be useful to do it in the implementation.
Like so:
void someFunc(int i);
void someFunc(const int i)
{
... do stuff involving 'i' ...
}
How do you do this? If I declare a function like you without the 'const' and then define it with 'const', I get a C2511: Overloaded member function not found in '...'
I see the benefit of it, but can't implement it. Confused here