C ++ future.get () return types

- Example updated -

Here's some code:

int retInt(int a) { return a; }
void randomFunction() 
{
    int a = 3;
    auto future = async([&]{ return retInt(a); });
    const auto ret = future.get();
}

      

VS2012 intellisense tells me what 'ret' is const < error-type >

and won't let me compile, giving me the output message:

[cannot type deduce for 'const auto' from 'void']

If, for example, I change 'ret' from const auto

to const int

and set the actual type, everything compiles just fine, but I'm wondering why the automatic version doesn't work, and if there was a possible code change of some kind to make the automatic compilation version.

Any ideas?

Note:

Change

auto future = async ([&] {return retInt (a);});

to

auto future = async ([&] () → int {return retInt (a);});

gives the same result

+3


source to share


1 answer


You have an extra []

inside the lambda expression that the inline lambda express does. the inner lambda returns 1

, but does not return the lamella return type, which is not valid by default.

change

auto afuture = async([&]{ []{ return 1; }; });

      

in

auto afuture = async( []{  return 1; });
const auto ret = afuture.get();

      

Edit:



Your new code just works fine on VS2012 NOV CTP and gcc 4.7.2.

Note: you capture the local variable a by reference, it is safe for asynchronous thread, you can capture it by value.

auto future = async([=]{ return retInt(a); });
                    ^^^

      

Compiled code example:

http://liveworkspace.org/code/X66xE $ 2

+2


source







All Articles