什么时候可以忽略C ++ 11 lambda中的返回类型? [英] When can we omit the return type in a C++11 lambda?

查看:228
本文介绍了什么时候可以忽略C ++ 11 lambda中的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至于我知道,在标准C ++ 11(不是C + +14 ),当省略lambda的返回类型时,其返回类型推导为:


  1. 在所有其他情况下,无论何时lambda只包含一个带有表达式的单一return语句,或

  2. void

现在考虑此代码:

  #include< iostream> 

auto closure = [](int x)
{
x ++;
return x;
};

int main()
{
int y = closure(10);
std :: cout<< y < std :: endl;
}

这应该属于情况2.,但是代码编译好像是C ++ 14与 auto 类型扣除,在g ++ 4.9.2,g ++ 5和clang ++中,与 -pedantic -Wall -Wextra -std = c ++ 11 。这里发生了什么?

解决方案

由于原始的C ++ 11限制被考虑,您的代码被接受而没有任何警告标准中的缺陷,它允许实现来修复行为。请参阅CWG DR975 DR1048 N3638


975。对lambdas返回类型扣款的限制



[2013年4月会议作为N3638号文件的一部分已移至DR状态。]



没有任何技术上的困难,需要当前的限制,只有当lambda的主体由一个return语句组成时,才能推导出lambda的返回类型。特别地,如果多个返回语句都返回相同的类型,则可以允许多个返回语句。



1048。自动扣除和lambda返回类型扣除。



...



2014年11月的会议:



CWG同意纸张 N3638中的更改应被视为针对C ++ 11


总而言之,DR975建议修改lambda表达式的返回类型扣除规则,以允许多个return语句。

DR1048标识了一个差异,其中使用占位符类型 auto 推导正常函数的返回类型的规则与DR975中提出的规则。具体来说,正常函数的返回类型推导在所有情况下都会丢弃顶级cv限定符,其中lambda表达式的cv限定符将保留类类型的cv限定符。



N3638解决了这个问题。






我怀疑有没有办法恢复原来的行为,在执行上述DR之前,C ++ 11 lambda支持的版本。


As far as I know, in standard C++11 (not C++14), when omitting the return type of a lambda, its return type is deduced to be:

  1. The type of the returned expression, whenever the lambda consists of nothing but a single return statement with an expression, or
  2. void in all other cases.

Consider now this code:

#include <iostream>

auto closure = [](int x)
{
    x++;
    return x;
};

int main()
{
    int y = closure(10);
    std::cout << y << std::endl;
}

This should fall under case 2., however the code compiles as if were C++14 with auto type deduction, in both g++4.9.2, g++5 and clang++, with -pedantic -Wall -Wextra -std=c++11. What's going on here? Am I interpreting the standard wrong?

解决方案

Your code is being accepted without any warnings because the original C++11 restriction is considered a defect in the standard, which allows implementations to fix the behavior. See CWG DR975, DR1048 and N3638.

975. Restrictions on return type deduction for lambdas

[Moved to DR status at the April, 2013 meeting as part of paper N3638.]

There does not appear to be any technical difficulty that would require the current restriction that the return type of a lambda can be deduced only if the body of the lambda consists of a single return statement. In particular, multiple return statements could be permitted if they all return the same type.

1048. auto deduction and lambda return type deduction.

...

Notes from the November, 2014 meeting:

CWG agreed that the change embodied in paper N3638 should be considered to have been a DR against C++11.

In summary, DR975 proposed modifying the rules for return type deduction for lambda expressions to allow multiple return statements.

DR1048 identifies a discrepancy where the rules for deducing the return type for normal functions using the placeholder type auto differs slightly from the rules proposed in DR975. Specifically, return type deduction for normal functions would discard top-level cv-qualifiers in all cases, where as those for lambda expressions would preserve cv-qualifiers for class types.

N3638 resolves this issue, amongst others.


I doubt there's any way to revert to the original behavior short of finding a compiler version that shipped with C++11 lambda support prior to the implementation of the DR above.

这篇关于什么时候可以忽略C ++ 11 lambda中的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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