samedi 13 février 2021

Is it possible to rebind the std::bind to another function with additional arguments

Consider you have some function that you wrapped using std::bind with some bounded arguments:

void func1(std::string& arg1, std::unique_ptr<int>& arg2)
{
    ...
}
...

auto f1 = std::bind(func1, std::string{"message"}, std::make_unique<int>(123));

Now, imagine for whatever reason you want to rebind this object to another function having some additional argument(s), for example:

void func2(std::exception& ex, std::string& arg1, std::unique_ptr<int>& arg2)
{
    ...
}
...

try {
    f1();
} catch (std::exception& ex) {
    auto f2 = some_magical_rebind(func2, std::move(ex), args_of(f1));
}

Is it possible to do that ? As std::bind uses std::tuple internally to pack the arguments it feels that should be possible.

Aucun commentaire:

Enregistrer un commentaire