CallBackAtInit
初期化時に関数を呼び出すことだけを目的とした名前のクラステンプレートを定義しました(コンストラクター)。関数はテンプレートパラメータで指定されます。std::function
問題は、テンプレートがパラメータとして受け入れられないことです。ただし、関数ポインタを受け入れます。なんで?
これが私のコードです:
#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;
}