Hey guys, i have a pointer-to-function variable which is supposed to store 'any type of function' with 'any amount of parameters'. I think this is possible so here is what i've tried so far;
#include <iostream>
usingnamespace std;
void Output(int num = 0) {
cout << num << endl;
}
class A {
private:
void (*funcptr)(void);
public:
template<typename ... Args>
void set(void func(Args... temp), Args... args) {
funcptr = new ((void) func(args...));
}
void invoke() {
funcptr(5); // should send the 'args...' but thats a question for later
}
};
void main() {
A firstfunc;
firstfunc.set(Output, 4);
firstfunc.invoke();
system("pause");
}
The code is obviously not working but it may help you understand what exactly i am going for in this. The question, in simpler words, is How do i store a function with any number of parameters, to a variable, with which i can call that function later?.
Instead of storing a function pointer you could use std::function to store a lambda that captures the function and all the arguments and calls the function when invoked.
Wow, thanks @Peter87, that worked like a charm. Just a small complication, still, i want to set() member function to take 'any' type of parameters, for some reason, it does not work for char * parameter;
#include <iostream>
#include <functional>
usingnamespace std;
class A {
private:
std::function<void()> fun;
public:
template<typename ... Args>
void set(void func(Args... temp), Args... args) {
fun = [=]() { func(args...); };
}
void invoke() {
fun();
}
void test()
{
cout << "Passed! " << endl;
}
};
void Output(int num = 0) {
cout << num << endl;
}
void names(char *name)
{
cout << name << endl;
}
void objects(A obj)
{
obj.test();
}
int main() {
A firstfunc, secfunc, thirdfunc;
firstfunc.set(Output, 6); // works
firstfunc.invoke(); // works
secfunc.set(objects, firstfunc); // works
secfunc.invoke(); // works
thirdfunc.set(names, "Ryan"); // does not work
thirdfunc.invoke(); // does not work
system("pause");
}
Actually, i checked int, custom class, float and some other parameters, it works in all of them, but not sure why it does not work for char *, and maybe there is some limitation to this. If so, please let me know.
The characters of a string literal are const (i.e. they cannot be modified) so the parameter of your names function should be a pointer to a const char.
1 2 3 4
void names(constchar *name)
{
cout << name << endl;
}
Note that the way the set function has been written the parameter types must match the types of arguments exactly. This will prevent you from passing an int to a function that takes a double, or a string literal to function that takes a std::string. To fix this you can add another template argument for the function type so that it becomes independent of the variadic argument list.
Alright i figured that out. Moreover, i was using typename template in .cpp (function definitions) file which was causing linker error. Fixed that as well.