lambda中的递归回调?

时间:2018-04-13 12:23:36

标签: c++ qt asynchronous lambda

如何使这样的东西起作用?

std::function<void()> infinity=[&]() ->void
{
    QPromise<void>([=](const QPromiseResolve<void>& resolve, const QPromiseReject<void>& reject) {
        QTimer::singleShot(25, [&]() {
            qDebug() << "step into infinity";
            resolve();
        });
    }).then([&]() {
        //infinity();
        QTimer::singleShot(225, [&]() {infinity();});
    });
};

infinity();

我一直收到分段错误错误或定时器无法调用函数

好的,这样可行,首先获取引用然后按值传递

std::function<void()> infinity=[&infinity]() ->void
{
    QPromise<void>([infinity](const QPromiseResolve<void>& resolve, const QPromiseReject<void>& reject) {
        QTimer::singleShot(25, [resolve](){
            qDebug() << "step into infinity";
            resolve();
        });
    }).then([infinity]() {
        //infinity();
        QTimer::singleShot(225, [infinity]() {infinity();});
    });
};

0 个答案:

没有答案