Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c++ - C++17 find exact arguments of function and pass to template function

I have a template function that I need to call, it looks something like this:

template<typename R, typename... A>
R fn_call(qword fn, A... args) {
    using T = R(*)(A...);
    fn = ...
    return ((T)fn)(args...);
}

#define wrapper_call(FUNC, ...) (fn_call<return_type_t<decltype(FUNC)>>((qword)FUNC, __VA_ARGS__))

Getting the return type for the function is simple, but I also need each argument. If I use it in its current state, the following will have issues because the stack parameters are not correct after r8.

void func(int a1, int a2, int a3, int a4, qword a5)

wrapper_call(func,0,0,0,0,0)

the 5th argument will be passed as a 4 byte int, when it must be a qword.

I am using C++17, and no standard library

question from:https://stackoverflow.com/questions/65879377/c17-find-exact-arguments-of-function-and-pass-to-template-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
template<class R, class...Args>
R fn_call_smart( R(*fn)(Args...), std::identity_t<Args>... args ) {
  return fn_call<R, Args...>(reinterpret_cast<qword>(fn), args...);
}

just call fn_call_smart, or rename it wrapper_call and strip out the macro.

fn_call_smart deduces the arguments from fn.

I don't see the point of your macro.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...