Specific case of unrolling an array to a function with arbitrary number and type of arguments

104 Views Asked by At

Important!

Existing answers imply that all function parameter types are the same, only the number of parameters varies.

For instance : Any Solution to Unpack a Vector to Function Arguments in C++?

It's not possible to use return func(args[I]...) from the solution since each parameter has to be converted to a specific type

The question

I have two well defined types:

class Dest;
class A;

I have a safe_vector template class that is a std::vector with a safevalue(i) function that returns a default constructed value if i is out of bounds.

I have a template function which returns a type T from an argument of type A.

template<typename T>
T factory(A a);

Now I have a set of arbitrary functions:

void f_a(Dest * dest, T1 a, T2 b, T3 c);
void f_b(Dest * dest, T2 a);
void f_c(Dest * dest, T4 a, T1 b);
void f_d(Dest * dest, T4 a, T1 b, T1 c, T3 d);
void f_e(Dest * dest, T4 a, T1 b);

And I need to give these functions as callback functions of type:

void * (Dest * dest, const std::safe_vector<A> & args)

For instance:

addCallback("f_a", [](Dest * dest, const std::safe_vector<A> & args)
                {
                    f_a(dest,
                        factory<T1>(args.safe_value(0)),
                        factory<T2>(args.safe_value(1)),
                        factory<T3>(args.safe_value(2))   );
                });

Is there any method of doing this with a variadic template function that will automatically deduce the argument list from the function signature, and properly do the argument unroll?

I have some ideas but they imply recursive lambda functions, which will be runtime recursive and that's ugly

The context

A is a variant type from a scripting language. Some function of that scripting language are hooked to C++ functions. The factory function just converts the variant type to the required C++ classes. There are a lot of functions to hook, hence the need for a clean, variable argument template to do the job. Currently C++ callbacks take the variant array as a parameter and extract/convert the values internally, which makes them poorly readable.

0

There are 0 best solutions below