Metaprogramming: Typedef of std::function argument list

  Kiến thức lập trình

I’m building a plugin for an engine, and I’m aiming to do as much templating and metaprogramming as I can because I will be making a version of this plugin for a different engine, as well.

To start with the first version, I have a set of function objects that are declared based on an API of function pointers (the pointers are removed for pure function signature). This is meant so I can register my function objects as callbacks to the engine’s signals, such as OnMissionLoad. With the function objects declared, next I need to build the functions that will be assigned into the function objects. This is where I’m trying to let the compiler do the work, but failing. I want to typedef the argument list of the function objects so I can make the function and pass in that typedef to automatically get the argument list without manually writing all the types. The types can be varied as are the number of arguments.

API Function object

std::function<std::remove_pointer_t<OnMissionLoad_Func>> OnMissionLoad;
-> std::function<APIResult(const char*)> OnMissionLoad;
class Plugin
{
protected:
        // Code I found online
        template <class T> struct std_func_param {};
        template <class R, class T> struct std_func_param<std::function<R(T)>> { using type = T; };
        template <class T> using std_func_param_t = typename std_func_param<T>::type;

        APIResult OnMissionLoad(std_func_param_t<decltype(VBSAPIFunctions::MissionListenerAPI::OnMissionLoad)> args) {}
};

The above works, because there is only one argument in the list. However, if I write another with two variables in the argument list, I will get

Error   C2794   'type': is not a member of any direct or indirect base class of 'Plugin::std_func_param<std::function<void (const void *,uint32_t)>>'

Error   C2938   'Plugin::std_func_param_t' : Failed to specialize alias template

Is there a way in C++20 to specialize a variable number of arguments so I can do as I did with the OnMissionLoad and effectively pass in the name of the function object to break it down to its argument list?

New contributor

Jonci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT