vendredi 2 juin 2023

How can a variadic macro with a list of types produce a function which accepts those arguments and passes them along?

I have a macro:

#define WRAP_FUNCTION(wrapper_name, function, ret_type, arg1_type, arg2_type, ...)

And I would like it to define a function like this:

ret_type wrapper_name(arg1_type arg1, arg2_type arg2, ...) {
    return function(arg1, arg2, ...)
}

Except where the ellipses (along with arg1 and arg2) are a dynamically sized list. I could create the function prototype using __VA_ARGS__, but that doesn't give me names for the arguments to pass to the wrapped function.

I assume the usual approach would be std::apply (or to copy-paste the implementation of std::apply, since it's not available in C++11). Unfortunately the argument function may be a macro or a compiler built-in so it cannot reliably be passed as a function type.

What I have right now is the generation of a variadic template function:

template<class... Args>
ret_type wrapper_name(Args... args) {
    return function(args...);
}

But this causes a world of problems with conversions because there's a lot that cannot be done with implicit conversions in this form when the wrapped function is overloaded.

Aucun commentaire:

Enregistrer un commentaire