出现这个错误基本由于Lambda中没有显示地说明返回值类型,而是由编译器自己推断返回值。下面是从C++11官方文档中抄过来的
If a lambda-expression does not include a trailing-return-type, it is as if the trailing-return-type denotes the following type:
-
If the compound-statement is of the form
{ return expression ; }
the type of the returned expression after lvalue-to-rvalue conversion (4.1), array-to-pointer conversion (4.2), and function-to-pointer conversion (4.3); -
otherwise,
void
.
如果lambda的主体中有多句语句,建议将其返回类型声明,否则编译器将会自行推断返回值,从而导致编译失败。
lambda添加返回值格式为:
[...] (...) mutableopt throwSpecopt ->retTypeopt {...}