代码之家  ›  专栏  ›  技术社区  ›  Fabian Knapp

lambda中嵌套的std::bind不能强制转换为std::function(从void*)

  •  0
  • Fabian Knapp  · 技术社区  · 7 年前

    我必须强制转换一个成员方法调用( SystemUpdater::OnSystemUpdateCompletedHandler

    代码段1:

    updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
                                    this->updateHelper->StartUpdate(resultFile, std::bind(&SystemUpdater::OnSystemUpdateCompletedHandler, this, std::placeholders::_1));
    

    代码段2:

    updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
                                this->updateHelper->StartUpdate(resultFile, [&](int result){OnSystemUpdateCompletedHandler(result); });
    

    StartUpdate的第二个参数在我的静态C函数中被强制转换和执行:

        (*((std::function<void(int)>*) userdata))(_result);
    

    此强制转换和执行适用于代码段2,但不适用于代码段1(信号:SIGSEGV(分段错误))。

    为什么?

    StartUpdate声明为:

    UpdateResult_t StartUpdate(const std::string &path,const std::function<void(int)> &finishCallback)
    
    0 回复  |  直到 7 年前
    推荐文章