0

I have main function which calls to download manager function. Inside of that function, I want to be able to use the pathUrl inside the onFileTaskSuccess function. How can i pass it in ?

std::string  MyClass::DownloadFromUrl()
{
    std::string pathUrl ="";
    //Then i have downloader which looks like this:
    this->m_downloader->onFileTaskSuccess = [this](const network::DownloadTask& task)
    {
        //i want to use pathUrl here .. how can i pass it to here ?
        pathUrl = someValueFromTheApp;
    }
return pathUrl;
}

EDIT:
My main goal is to return value from the main function i fixed the question
which is calculated in the inner lambda function. i also tried : this by reference:

this->m_downloader->onFileTaskSuccess = [this,&pathUrl,&someValueFromTheApp](const network::DownloadTask& task)
        {
            //i want to use pathUrl here .. how can i pass it to here ?
            pathUrl = someValueFromTheApp;
        }   

but im getting this error:

error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) 
3
  • You can check What is a lambda expression in C++11? for general and the specific information you are looking for. Commented Dec 16, 2016 at 13:21
  • Also, there is a StackOverflow document on C++ lambda Commented Dec 16, 2016 at 13:22
  • this is not working in my case Commented Dec 16, 2016 at 16:35

1 Answer 1

4

Capture pathUrl inside the lambda:

this->m_downloader->onFileTaskSuccess = [this, pathUrl](const network::DownloadTask& task)
{
    // `pathUrl` can now be used.
}

In general, you can choose between capturing by value (i.e. making a copy) or by reference. If you capture by reference, you're assuming that the captured object will live at least as long as the lambda.

In your particular case it seems like onFileTaskSuccess is an asynchronous function that will be called after DownloadFromUrl's scope ends - therefore pathUrl should be captured by value.


In C++14 you may avoid an unnecessary copy by using generalized lambda captures:

this->m_downloader->onFileTaskSuccess = [this, pathUrl = std::move(pathUrl)]
    (const network::DownloadTask& task)
    {
        // `pathUrl` can now be used.
    }

(The code above assumes that pathUrl will not be used after assigning onFileTaskSuccess to the lambda.)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks allot , how do i capture by reference ?
problem when i try by reference like this : >onFileTaskSuccess = [this, &pathUrl,&DownloadPath](const network::DownloadTask& task) { pathUrl = DownloadPath } gives me error : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
Is pathUrl declared as const?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.