I have defined class template named CallBackAtInit
which only purpose is to call a function at its initialization (constructor). The function is specified in template parameters. The problem is that templates does not accept std::function
as parameters; but they accept function pointers. Why?
Here is my code:
#include <iostream>
#include <functional>
/* Does not work:*/ template <typename return_type, typename arg_type, std::function<return_type(arg_type)> call_back>
/* Work fine: */// template <typename return_type, typename arg_type, return_type(*call_back)(arg_type)>
class CallBackAtInit {
public:
CallBackAtInit(arg_type arg)
{
call_back(arg);
};
};
void printInt(int i);
class HoldInt : private CallBackAtInit<void, int, printInt> {
public:
HoldInt(int integer)
: CallBackAtInit(integer)
{}
// ...
};
int main(int argc, char** argv)
{
HoldInt hi(10);
return 0;
}
void printInt(int i)
{
std::cout << i << std::endl;
}