I'm stuck with variadic template problem.
Contexte
On one hand, I use a library whose functions look like
template<class ...Cols>
void doForVars(Cols&... args) {
...
}
And on the other hand I have a lot of structs
struct Foo {
int id;
std::string name;
double length;
}
struct Bar {
int id;
double someProperty;
double someOther;
}
I have for each of them to use a specialized template
template<>
void doSomething(Foo& foo) {
//boilerplate
doForVars( foo.id, foo.name, foo.length);
}
template<>
void doSomething(Bar& bar) {
//boilerplate
doForVars( bar.id, bar.someProperty, bar.someOther);
}
Problem and expectations
As I get more than 50 types like Foo and Bar with 10 to 30 fields, there a lot of boilerplate to copy and paste.
It's error-prone and such a violation of DRY that I can stand for it :)
So, I'm searching for to create a template for a function listFields(T& t) that just list fields of record T and whose return value is acceptable for doForVars(Cols...&cols).
template<typename T>
void doSomething(T& t) {
//boilerplate
doForVars(listFields(t));
}
But I don't succeed to write this template returnint a variadic type.
template<typename T, ?>
<I don't know> listFields(T& t);
such as I could just write
template<>
<I don't know> listFields(Foo& foo) {
return <I don't know>( foo.id, foo.name, foo.length);
}
template<>
<I don't know> listFields(Bar& bar) {
return <I don't know>( bar.id, bar.someProperty, bar.someOther);
}
variadic are shown as input, but how to use them for output ?
Thanks by advance.
EDIT
Thank you for your answers I'm reading. As I wrote, I get a lot of record to describe and use ([SCOS 2000 MIB records types])1
The library I use for variadic is this jewel https://github.com/d99kris/rapidcsv
I use it to parse some tabulation separated value files and map each row to my records. Pretty smooth.