How to guide not compiling on VS2010

Mar 28, 2013 at 10:45 AM
Hello,

I tried to compile the example in How to: Work with Asynchronous Streams on VS2010 and I got the following errors:

pplxtasks.h(385): error C2338: incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below)
          pplxtasks.h(400) : see reference to class template instantiation 'pplx::details::_FunctionTypeTraits<_Function,_ExpectedParameterType>' being compiled
          with
          [
              _Function=int,
              _ExpectedParameterType=size_t
          ]
          example.cpp(66) : see reference to class template instantiation 'pplx::details::_ContinuationTypeTraits<_Function,_ReturnType>' being compiled
          with
          [
              _Function=int,
              _ReturnType=size_t
          ]
example.cpp(58): error C3499: a lambda that has been specified to have a void return type cannot return a value
example.cpp(66): error C3499: a lambda that has been specified to have a void return type cannot return a value
All errors involve the lambda expressions in the HTTPStreamingAsync() function. The last 2 can be fixed by adding the lambda return type, like this:

return client.request(methods::GET).then([](http_response response) -> pplx::task<void> 
    { 
        ... 
    });
However, I have not been able to find a solution for the first problem. It looks like the compiler can't resolve the type of the lambda expression passed as a continuation for the task returned from read():

return bodyStream.read(inStringBuffer, 15).then([inStringBuffer](size_t bytesRead)
        {
            const std::string &text = inStringBuffer.collection();

            // For demonstration, convert the response text to a wide-character string.
            std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv;
            std::wostringstream ss;
            ss << utf16conv.from_bytes(text.c_str()) << std::endl;
            std::wcout << ss.str();
        });
Is there any workaround or fix?
Mar 28, 2013 at 2:48 PM
Edited Mar 28, 2013 at 2:50 PM
Seems like we didn't check the sample with VS 2010. That version of the compiler has trouble with deducing the return type of non-trivial lambdas, so the return type should have been specified.

Let me take a look at this and see what I can figure out when I get to the office.

Thanks,

Niklas
Mar 28, 2013 at 4:18 PM
Edited Mar 28, 2013 at 4:21 PM
Nested lambdas.

VS 2010 has trouble with nested lambdas, and so the fix is to unnest the lambda. The following at least compiles, but I haven't tried running it.

Niklas
// Creates an HTTP request and prints part of its response stream.
pplx::task<void> HTTPStreamingAsync()
{
    http_client client(L"http://www.fourthcoffee.com");

    streams::container_buffer<std::string> inStringBuffer;

    auto nested = 
        [inStringBuffer](size_t)
        {
            const std::string &text = inStringBuffer.collection();

            // For demonstration, convert the response text to a wide string.
            std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv;
            std::wostringstream ss;
            ss << utf16conv.from_bytes(text.c_str()) << std::endl;
            std::wcout << ss.str();
        };

    return client.request(methods::GET).then(
        [inStringBuffer, nested](http_response response) -> pplx::task<void>
        {
            if(response.status_code() != status_codes::OK)
            {
                // Handle error cases... 
                return pplx::task_from_result();
            }

            // Perform actions here reading from the response stream... 
            // In this example, we print the first 15 characters of the 
            // response to the console.
            streams::istream bodyStream = response.body();
            return bodyStream.read(inStringBuffer, 15).then(nested);
        });
}
Mar 28, 2013 at 4:43 PM
OK, it runs fine.
Thank you for the quick answer!