I'm dealing with code, that has multiple structures consisting of string-fields only. For example:
struct record {
string first;
string second;
string third;
string Nth;
};
These are currently all loaded from a database and stored in different map<string, somestruct> -- with a separate method for each. The methods are the same (except for the SQL-queries and field-names) -- the first field of each query becomes the map's index, and the subsequent ones form the structure's fields:
void
LoadFoo()
{
rows = RunSQLQuery("SELECT key, first, second FROM sometable");
for (auto row = rows.cbegin(); row != rows.cend(); row++) {
struct record r = {
first = row[1];
second = row[2];
};
Foos.insert(make_pair(row[0], r));
}
}
I'd like to unify these methods into one, capable of initializing structures with different number of fields -- and differing field-names.
This new method will receive the SQL-query to run and the (empty) map. The method will know the total number of fields the query returns, but it would not be able to refer to individual fields by name -- only by sequence number.
Which would be Ok, if the above structures can be safely cast into string[N]. Can they?
(No, I don't want to change from structs to arrays "officially", because the field-names are meaningful where they are actually used throughout the rest of the application.)