lundi 12 septembre 2022

Is there a way to remove the last template parameter of variadic template methods?

I'm working on creating a simple reflector in C++11, it stores function pointers of instances functions as:

static std::unordered_map<std::string, std::pair<void(EmptyClass::*)(void), int>>* methods;
template<typename ClassType, typename returnType, typename... Args>
static void RegistFunction(std::string name, returnType(ClassType::* func)(Args... args)) {
    (*methods)[name] = std::make_pair((void(EmptyClass::*)())func, sizeof...(Args));
}

template<typename ReturnType, typename ClassType, typename... Args>
static ReturnType ExecuteFunction(ClassType* object, std::string name, Args... args) {
    if (object == NULL) return;
    ReturnType(ClassType:: * func)(Args...) = (ReturnType(ClassType::*)(Args...))(*methods)[name].first;
    return (object->*func)(std::forward<Args>(args)...);
}

But when I want to call ExecuteFunction, the number of arguments may be more than the number that function pointer actually accepts. So I need to remove some arguments from the tail of argument list, but it seems I can only remove from head.

template<typename ReturnType, typename ClassType, typename Arg, typename... Args>
    static ReturnType ExecuteFunction(ClassType* object, std::string name, Arg arg, Args... args) {
        if (sizeof...(Args) + 1 > (*methods)[name].second) {
            return ExecuteFunction<ReturnType>(std::forward<ClassType*>(object), std::forward<std::string>(name), std::forward<Args>(args)...);
        }
        if (object == NULL) return;
        ReturnType(ClassType:: * func)( Arg, Args...) = (ReturnType(ClassType::*)(Arg, Args...))(*methods)[name].first;
        return (object->*func)(std::forward<Arg>(arg), std::forward<Args>(args)...);
    }

Is there any solution to remove arguments at the tail of variadic method template?

Aucun commentaire:

Enregistrer un commentaire