有没有理由不允许lambdas推导返回类型,如果它包含多个语句? [英] Is there a reason on not allowing lambdas to deduce the return type if it contains more than one statement?

查看:193
本文介绍了有没有理由不允许lambdas推导返回类型,如果它包含多个语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取自C ++ 0x FDIS(n3290):


如果lambda表达式不包含lambda声明,它就像lambda声明符是()。如果一个lambda表达式不包含一个尾部返回类型,它就好像tail-return-type表示以下类型:




  • 如果复合语句的格式为

    {attribute-specifier-seq opt return expression; }

    在左值到右值转换(4.1),数组到指针转换
    (4.2)和函数到指针转换(4.3)之后返回的表达式的类型;

  • 否则为无效。


标准允许编译器根据第一个找到的 return 语句来分析复合语句并确定返回类型?



例如:

  int main(){
// compiler:nope.jpg
auto l = [] {
//一个计算
//另一个计算
//另一个计算
return something;
}
}

编辑: 因为标准说这么答案。 :)

解决方案

没有技术上的原因。 IIRC它已经由GCC C ++维护者实施,他说它实施起来很琐碎。



委员会非常保守地接受标准中的功能,所以他们用这种简单的扣除形式,并希望接受一个更强大的形式。查看美国30 评论遭拒的原因。



DR 975 已经标记为就绪,因此可能会被接受。


Taken from the C++0x FDIS (n3290):

If a lambda-expression does not include a lambda-declarator, it is as if the lambda-declarator were (). 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
    { attribute-specifier-seqopt 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.

Why doesn't the standard allow the compiler to analyse the compound-statement and determine the return type based on the first found return statement?

I can't see any reason to not allow this, but maybe I'm overlooking something.

Example:

int main(){
  // compiler: nope.jpg
  auto l = []{
    // one computation
    // another computation
    // yet another one!
    return something;
  }
}

Edit: Please no "because the standard says so" answers. :)

解决方案

There's no technical reason for this. IIRC it was already implemented by the GCC C++ maintainer and he said it's trivial to implement.

The committee is very conservative about accepting features into the Standard so they went with this simple form of deduction and will later hopefully accept a more powerful form. See the reason for rejection on US 30 comment.

DR 975 is already marked "ready" so chances are good it will be accepted.

这篇关于有没有理由不允许lambdas推导返回类型,如果它包含多个语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆