0

I am trying to understand the following code but I'm stuck at

[=](ostream outFile){}

this parameter is just declaring a new empty variable/class? or is getting any previous ostream value?

If possible, please explain as much as you can because i'm new with lambdas

#include "stdafx.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(int argc, char* argv[])

    auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.txt"))
        .then([=](ostream outFile){
            *fileStream = outFile;

            // Create http_client to send the request.
            http_client client(U("https://localbitcoins.com"));

            // Build request URI and start the request.
            uri_builder builder(U("/api/payment_methods/"));

            //builder.append_query(U("q"), U("cpprestsdk github"));
            return client.request(methods::GET, builder.to_string());
        })

        // Handle response headers arriving.
        .then([=](http_response response){
            printf("Received response status code:%u\n", response.status_code());

            // Write response body into the file.
            return response.body().read_to_end(fileStream->streambuf());
        })

        // Close the file stream.
        .then([=](size_t){
            return fileStream->close();
        });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

    return 0;
}
Armando
  • 31
  • 4
  • http://en.cppreference.com/w/cpp/language/lambda – Tyker May 04 '18 at 12:50
  • Since the parameter is taken by value it's creating a new `ostream` object which presumably will be move-initialized from some other ostream. – David G May 04 '18 at 12:50
  • 3
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver May 04 '18 at 12:50

0 Answers0