Using n arguments from Args… starting from position m

为君一笑 提交于 2019-12-13 02:22:45

问题


Say I have different functions, which have a variable number of arguments. The first argument is always a pointer obtained through other means. All other arguments I obtain through another template using template pack expansion.
Template that I use for calling these functions is as follows:

template<typename RT, typename... Args>
inline RT call(RT(*function)(Args...))
{
    return function(pointer_from_somewhere, bind_argument<Args>::get_arg()...);
}

This obviously doesn't compile, as it performs template expansion for all arguments, thus there being too many arguments.
Since I always obtain the first argument through other means, how would I do template pack expansion for sizeof...(Args) - 1 arguments, starting from the 2nd argument?

EDIT:
While the template is slimmed down for demonstration purposes, it might be relevant, that the 1st argument (the pointer) is reinterpret_cast'ed always to the type of first argument. I use std::tuple_element<0, std::tuple<Args...>>::type for finding out the type of the 1st argument.


回答1:


Is this what you are looking for?

template<typename RT, typename A0, typename... Args>
inline RT call(RT(*function)(A0, Args...))
{
    return function(pointer_from_somewhere, bind_argument<Args>::get_arg()...);
}


来源:https://stackoverflow.com/questions/36217632/using-n-arguments-from-args-starting-from-position-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!