However, if a lambda has one statement and that statement is a return statement (and it returns an expression), the compiler can deduce the return type from the type of that one returned expression. You have multiple statements in your lambda, so it doesn't deduce the type.
OK, clear as a crystal but why on this MSDN site they write this
1 2 3 4
auto t = create_task([]() -> int
{
return 42;
});
Why does the Microsoft overcomplicate samples? Doesn't it? Maybe I'm wrong or I just don't get it, but it should be without
In this case the -> int is redundant, but not necessarily without purpose. For someone casually reading the code it helps make it clear that the intent is for the lambda to return an int. Consider this:
1 2 3 4
auto t = create_task([]()
{
return foo();
});
Without the explicit type and without looking up what foo() is, you can't tell what this lambda returns.