#include <iostream>
#include <functional>
template<typename Ret, typename ...Params>
struct sick
{
template<std::function<Ret(Params...)>*fun>
struct man
{
static Ret call_it(Params... param)
{
return (*fun)(param...);
}
};
};
void needs_a_void_int(void (*fun)(int))
{
fun(123);
}
void needs_a_int_int_int(int (*fun)(int, int))
{
std::cout << fun(123,231) << std::endl;
}
class Example
{
public:
void bla(int x)
{
std::cout <<__FUNCTION__ << ' '<< x << std::endl;
}
int plus(int y, int x)
{
std::cout <<__FUNCTION__ << y << '+'<< x << std::endl;
return x+y;
}
};
// the following functions need to be global
// otherwise the address would not be a constant expression
// (in c++03 they would have have external linkage)
std::function<void(int)> bla;
std::function<int(int,int)> plus;
int main()
{
Example obj;
bla = [&obj](int value)->void { obj.bla(value); };
plus = [&obj](int v1, int v2)->int { return obj.plus(v1,v2); };
// with function traits that could be a macro
needs_a_void_int(&sick<void,int>::man<&bla>::call_it);
needs_a_int_int_int(&sick<int,int,int>::man<&plus>::call_it);
}