vendredi 22 février 2019

std::function as argument of std::function

for example, I can do something like:

void foo(int i)
{
    std::cout << "fo is called with: " << i << "\n";
}

int main()
{
    std::function<void(int)> fo = std::bind(&foo, std::placeholders::_1);
    fo(5);
}

and output

fo is called with: 5

now I'm curious about if I can make a std::function that receiv ae std::function as argument,so I think of some function like:

void foAndFo(function<void(int)> fo)
{
    std::function<void(int)> fo = std::bind(&foo, std::placeholders::_1);
    fo(5);
}

but this can even not been compiled.

now I changed to something may call function pointer.

using foType = void(*)(int);
void foAndFo(foType fo)
{
    std::function<void(int)> fo = std::bind(&foo, std::placeholders::_1);
    fo(5);
}

now it can be compiled.But now I cannot std::bind it like:

std::function<foType> fo2 = std::bind(&foAndFo, std::placeholders::_1); //error

is it possible to use std::function as argument of std::function?

Aucun commentaire:

Enregistrer un commentaire