lambdas被编译成什么类型​​? [英] What type do lambdas get compiled into?

查看:95
本文介绍了lambdas被编译成什么类型​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我知道所有的数据类型必须在编译时知道,lambda不是一个类型。 lambda是否被转换为 anonymous struct with operator() std :: function wrapped?



例如,

  std :: for_each(v.begin(),v.end (),[](int n&){n ++;}); 

解决方案

as-if规则,C ++ 11标准说:


§5.1.2/ 3 [..] 一个实现可以定义闭包类型
与下面描述的不同,前提是这不改变
程序的可观察行为,而不是改变:



- 闭包类型的大小和/或对齐方式,



- 闭包类型是否是可简单复制的(第9条),



- 闭包类型是否是一个标准布局类(第9条) >

- 封闭类型是否为POD类别(第9条)。


相信这是人们的意思,当他们说,未指定。但是,其他答案中已经说明的保证如下:



原作者: Lightness Races in Orbit



:5.1.2 / 3]: lambda-expression 的类型(也是闭包对象的类型)是一个唯一的未命名的
非联合类类型
- 称为闭包类型 - 其属性
如下所述。此类类型不是聚合(8.5.1)。
闭包类型在最小的块范围,类范围或
命名空间范围中声明,它包含相应的lambda表达式。
[..]


子句继续列出此类型的不同属性。这里有
一些亮点:


[C ++ 11:5.1.2 / 5] lambda-expression 的闭包类型有一个public inline 函数调用操作符
返回类型由 lambda-expression
参数声明子句 trailing-return-type [..]



[C ++ 11:5.1.2 / 6]:不具有 lambda捕获表达式的闭包类型具有公共非虚拟非显式const
转换函数,相同的参数
和返回类型作为闭包类型的函数调用操作符。这个转换函数返回的
值应该是一个
函数的地址,当被调用时,它与调用
闭包类型的函数调用操作符具有相同的效果。




As I know all data types must be known at compile time, and lambda is not a type. Does lambda got translated into anonymous struct with operator() or std::function wrapped?

For example,

std::for_each(v.begin(), v.end(), [](int n&){n++;});

解决方案

A variation of the as-if rule, the C++11 standard says:

§5.1.2/3 [..] An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:

— the size and/or alignment of the closure type,

— whether the closure type is trivially copyable (Clause 9),

— whether the closure type is a standard-layout class (Clause 9), or

— whether the closure type is a POD class (Clause 9).

I believe this is what people mean when they say that it's unspecified. However what's guaranteed as already stated in the other answers is the following:

Original author: Lightness Races in Orbit

[C++11: 5.1.2/3]: The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression. [..]

The clause goes on to list varying properties of this type. Here are some highlights:

[C++11: 5.1.2/5]: The closure type for a lambda-expression has a public inline function call operator (13.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively. [..]

[C++11: 5.1.2/6]: The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.

这篇关于lambdas被编译成什么类型​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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