I am working with a third party library that passes values using a custom vector and variant types. These vectors however map 1:1 to my functions. My end goal is to reduce the amount of boilerplate.
A contrived example:
#include <functional>
// A third party typed I can't change
struct dumb_varient_type
{
int to_int() { return 3; }
double to_double() { return 3.14; }
std::string to_string() { return "Pi"; }
};
template< class R, class... Args >
R do_callback(std::function<R(Args...)> func, std::vector<dumb_varient_type> args )
{
// call func, with the correct types
return func( vector_of_pain_to_expanded_parameter_pack_or_something(args) );
}
int main(int argc, char **argv) {
std::vector<dumb_varient_type> arg1 = get_args();
auto c1 = [](int, double) {/*do work*/};
auto c2 = [](double, std::string) {/*do work*/};
auto val1 = do_callback( c1, args );
auto val2 = do_callback( c2, args );
// std::vector< dumb_varient_type > arg2( /* Anything*/ );
// std::function<int(/*Anything*/)> c3;
// auto val3 = do_callback( c3, arg2 );
}
I don't need to worry about ambiguity; The argument list does not need to be routed or dispatched to the correct function. I know the intended callback. But as the argument list created at runtime there could theoretically be a mismatch in arg count.
How would one implement do_callback (or equivalent)
Aucun commentaire:
Enregistrer un commentaire