Reputation: 49
I want to create a function like thread()
function (get N parameters). I think I have to use a template like this:
template <typename Func, typename Param1, ...>
void function(params)
{
}
But for my function, I have to set a limited number of parameters. In the thread()
function you can give this function N parameters. How is it possible to create a function and give the parameters same inside function params?
template <typename Func, typename Param1, typename Param2, typename Param3 = typename()>
void Remote_function_caller(Func Function_name, Param1 P1, Param2 P2, Param3 P3 = Param3())
{
Function_name(P1, P2, P3);
}
void Tool1(int Data1, int Data2)
{
}
void Tool2(int Data1, int Data2, int Data3)
{
}
void main()
{
// Call tool 1
Remote_function_caller(Tool1, 5, 6);
// Call tool 2
Remote_function_caller(Tool2, 5, 6, 9);
}
To call Tool1
I have to enter 2 parameters but system gives an error because caller needs 3 parameters (and load that function with 3 parameters)...
Upvotes: 1
Views: 63
Reputation: 17483
I suggest using variadic templates:
template<typename Func, typename... Params>
void function_caller(Func func, Params&&... params) {
func(std::forward<Params>(params)...);
}
Short example:
#include <iostream>
#include <utility>
template<typename Func, typename... Params>
void function_caller(Func func, Params&&... params) {
func(std::forward<Params>(params)...);
}
void foo(int x, int y) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void bar(int x, int y, int z) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main() {
function_caller(foo, 5, 6);
function_caller(bar, 5, 6, 9);
return 0;
}
The output is:
void foo(int, int)
void bar(int, int, int)
Upvotes: 2