I'm loading various data from database, and have already implemented a template-method:
template <typename R>
bool
loadMap(map<const string, R> &store, const char *query) {
{
...
for (auto row : rows(query)) {
store.insert(make_pair(row[0], R(row[1], row[2], ....));
}
}
The method sends the query
to the DB-server and populates the store
with the results. The first column becomes the key of the map, and the rest of the fields form the value structure. That works.
However, in a couple of cases, the queries returns just one column -- which is stored in a set
(or unordered_set
) -- without any values associated with it.
Currently those cases are handled by methods of their own, and I'd like to extend my loadMap
-function to these other types of containers -- perhaps, renaming it to loadContainer
.
How would this new loadContainer
's declaration look, and how would it invoke the store.insert()
with just the key, no values?
(I'm aware of the dodge of turning the set into a map with all values being empty, but I'd like to avoid that.)
Actually, I cannot even think of a way to handle map
and unordered_map
-- though the insert
would be exactly the same, how would I declare such a function? The two map-types don't seem to have a common ancestor (nor "interface", to use a Java-term)...