lundi 1 janvier 2018

std::function accepting different function signatures in c++

I'm trying to create a c++ event system similar to c# system. I need to be able to store any kind of functions and call them with the right parameters at the appropriate time.

The closest I can get is with std::function, bind and placeholders but here is my problem.

void Func()
{
    std::cout << "Notified" << std::endl;
}

void FuncWithParam(const std::string& str)
{
    std::cout << str << std::endl;
}

std::function<void()> fn = std::bind(Func); // this works

std::function<void()> fn = std::bind(FuncWithParam, "Hello there"); // this works also

std::function<void()> fn = std::bind(FuncWithParam, _1); // but this doesn't

Is it actually possible to store any kind of signature in a single std::function ? Or do I have to recourse to a more complex solution.

Aucun commentaire:

Enregistrer un commentaire