I have questions with below code snippet, not sure if I correctly understand the codes.
template <typename R, typename... Args>
class RunnableAdapter<R(*)(Args...)> {
public:
typedef R (RunType)(Args...);
explicit RunnableAdapter(R(*function)(Args...))
: function_(function) {
}
R Run(Arg... args) {
return function_(args...);
}
private:
R (*function_)(Args...);
};
<R(*)(Args...)>
is a "type of function pointer"and blink space between R and (*) is not necessarily required?and what could instanciation of RunnableAdapter be? I assume it is like below.
void myFunction(int i){ // }; RunnableAdfapter<(void)(*)(int)> ra(MyFunction); ra.Run(1); //which calls MyFunction(1)
At first the code you provided have some mistakes and does not even compile. To answer tou questions:
You could declare you class like this
And instantiate it
But you could simplify it (here is full working example)
This would allow instantiation with signature-like expressions like
void(int)
. It just looks better, no need in(*)
.Also here is another way is to do it without class specialization, like this. The result is the same, but class declaration and instantiation is slightly different.
EDIT
As @Jarod42 proposed it is better to make
Run
like this