取自C++ 0x FDIS(n3290):
如果lambda表达式不包含lambda声明符,那就好像lambda声明符是().如果lambda表达式不包含trailing-return-type,则就好像trailing-return-type表示以下类型:
- 如果compound-statement的格式为
{attribute-specifier-seq opt return expression; }
返回的表达的左值到右值转换(4.1),阵列到指针转换(4.2),和功能到指针转换(4.3)后的类型;- 否则,无效.
为什么标准不允许编译器分析复合语句并根据第一个找到的return语句确定返回类型?
我看不出任何理由不允许这样,但也许我忽视了一些事情.
例:
int main(){
// compiler: nope.jpg
auto l = []{
// one computation
// another computation
// yet another one!
return something;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:请不要"因为标准是这么说的"答案.:)