4 ms·
At my previous job I created wrappers around QSqlQuery that don't work with string keys in operator[] and instead used template argument pack decomposition. So
by eska 7y ago
At my previous job I created wrappers around QSqlQuery that don't work with string keys in operator[] and instead used template argument pack decomposition. So given a function
DbResultIterator<int, QString> getUserNames()
I could do something like this:
for(auto [id, name] : db.getUserIdsAndNames()) {
// id and name are type-safe int and QString
}
vector<QString> v;
ranges::transform(db.getUserIdsAndNames(), [](int id, const QString& name) {
return name.toUpper()
}, back_inserter(v));
Internally the iterator would wrap QSqlResult and decompose the template argument pack, calling specialized functions such as
result.data(0).toString()
knowing the index and type due to the definition of DbResultIterator<...>.
Unfortunately I cannot share the code here because of copyright, but I can recommend it.