lundi 19 octobre 2020

Is there any issue if local std::function out of its "life"?

Could you please help to review the below code? is there any issue if a local std::function is out of its "life"? Thanks in advance.

class Test {
    public:
    void commit(std::function<void()> func)
    {
        // return immediately
        dispatch_async(conqueue, ^{
            // in order to call func when it is out of "life"
            sleep(5);
            func(); // is there any issue? the func should be invalid at this moment?
        });
    }

    void test() {
        std::string name = "test";
        std::function<void()> func = [namel, this] () {
            printf("%lx %s\n", &name, name.c_str());
            std::cout << "callback"<<std::endl;
        };
        // async
        commit(func);
    }
    //...
};

Aucun commentaire:

Enregistrer un commentaire