I devoloped this method whilst finding a way to nicely abstract binding a struct to a SQL statement for a SQLite wrapper, my aim is to be able to abstract away most of the binding process as well as being able to "alias" the specialised function template so that you dont need to retype the bindings on each use.
The method I found will compile on clang but I cant currently get it to work for both GCC and MSVC due to the use of the auto keyword this is replicated in the example below with add_2.
Example:
// final function to perform the addition on each member
template<class T, class M>
void add2_member(T& value, M member)
{
value.*member += 2;
}
// variadic template function that unpack the Members and calls add2_member for each
// arg in args using c++17 fold syntax.
template<class T, auto T::*... Members>
void add2(T& value)
{
(add2_member(value, Members), ...);
}
// example struct X
struct X
{
int a;
int b;
char c;
};
// alias add2 function specialisation
auto add2_x = add2<X, &X::a, &X::c>;
int main()
{
X x;
x.a = 2;
x.c = 1;
add2_x(x);
}
A live link showing this work is here which shows the example in godolt compiling in with Clang 9, but GCC is unable to deduce the type of the auto and I currently have not found a way to achive my goal without it, less this could be done using macro's.