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) 
user63898
  • 29,839
  • 85
  • 272
  • 514
  • You can check [What is a lambda expression in C++11?](http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) for general and the specific information you are looking for. – Dusteh Dec 16 '16 at 13:21
  • Also, there is a StackOverflow document on [C++ lambda](http://stackoverflow.com/documentation/c%2b%2b/572/lambdas/1856/capture-by-value#t=201612161319204117505) – Lyth Dec 16 '16 at 13:22
  • this is not working in my case – user63898 Dec 16 '16 at 16:35

1 Answers1

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.)

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • Thanks allot , how do i capture by reference ? – user63898 Dec 16 '16 at 15:54
  • Read this: http://en.cppreference.com/w/cpp/language/lambda#Lambda_capture – Vittorio Romeo Dec 16 '16 at 15:56
  • 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) – user63898 Dec 16 '16 at 16:05
  • Is `pathUrl` declared as `const`? – Vittorio Romeo Dec 16 '16 at 16:09