As an exercise, I recently implemented a simple settings reader class
which reads setting values from an INI file into an std::map<std::string, std::string>
. The reader method looks like the following:
std::map<std::string, std::string> readSettings() {
std::map<std::string, std::string> settings;
for(std::string rawSetting; std::getline(settingsFile, rawSetting); )
settings.insert(parseRawSettingIntoPair(rawSetting));
return settings;
}
I feel a bit redundant specifying the type of renturn value twice:
- firstly in the method declaration,
- secondly when declaring the local variable,
settings
.
I know that in c++14 the compiler can deduce the return type if I use the auto
specifier in the declaration of readSettings()
, but this feature will force me to keep its definition in the header file too. Is there any better approach to reduce such repetitions?
SettingsReader
class is to read settings from file into a container, but eventually it could be the container itself which hides how become filled from file.