mardi 29 janvier 2019

assign std::unique_ptr to std::function

There is a custom defined map, with an element std::function()>. The lambda code is working, but I don't know how to expand it to a normal formation. The code is following.

class TestA{
public:
    TestA() {}
    ~TestA() {}

    TestA(const TestA &) {}

    static void print()
    {
        cout << __FUNCTION__ << endl;
        return;
    }
};

void testComplexMap1()
{

    typedef map<string, std::function<std::unique_ptr<TestA>()>> TempMap;
    TempMap m;
    // the lambda format code, it works
    //m.insert({ "TestA", []() {return std::unique_ptr<TestA>(new TestA());}});

    // I want to expand it, but failed.
    TestA *t = new TestA();
    //function<unique_ptr<TestA>()> fp(unique_ptr<TestA>(t));
    function<unique_ptr<TestA>()> fp(unique_ptr<TestA>(t)()); //warning here
    //m.emplace("TestA", fp);              // compile error here
}

Any help will be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire