一个正 lambda:'+[]{}' - 这是什么魔法? [英] A positive lambda: '+[]{}' - What sorcery is this?

查看:22
本文介绍了一个正 lambda:'+[]{}' - 这是什么魔法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在堆栈溢出问题重新定义 C++11 中不允许的 lambda,为什么?,一个给出了无法编译的小程序:

In Stack Overflow question Redefining lambdas not allowed in C++11, why?, a small program was given that does not compile:

int main() {
    auto test = []{};
    test = []{};
}

问题得到了回答,一切看起来都很好.然后是 Johannes Schaub 并制作了 一个有趣的观察:

The question was answered and all seemed fine. Then came Johannes Schaub and made an interesting observation:

如果你在第一个 lambda 之前放一个 +,它会神奇地开始工作.

If you put a + before the first lambda, it magically starts to work.

所以我很好奇:为什么下面的方法有效?

So I'm curious: Why does the following work?

int main() {
    auto test = +[]{}; // Note the unary operator + before the lambda
    test = []{};
}

它在 GCC 4.7+ 和 Clang 3.2+.代码标准是否符合?

It compiles fine with both GCC 4.7+ and Clang 3.2+. Is the code standard conforming?

推荐答案

是的,代码符合标准.+ 触发转换为 lambda 的普通旧函数指针.

Yes, the code is standard conforming. The + triggers a conversion to a plain old function pointer for the lambda.

这是怎么回事:

编译器看到第一个 lambda ([]{}) 并根据 §5.1.2 生成一个闭包对象.由于 lambda 是一个非捕获 lambda,因此以下适用:

The compiler sees the first lambda ([]{}) and generates a closure object according to §5.1.2. As the lambda is a non-capturing lambda, the following applies:

6 没有 lambda-capturelambda-expression 的闭包类型具有公共非虚拟非显式 const 转换函数指向与闭包类型的函数调用运算符具有相同参数和返回类型的函数的指针.这个转换函数返回的值应该是一个函数的地址,这个函数在调用时与调用闭包类型的函数调用运算符具有相同的效果.

5.1.2 Lambda expressions [expr.prim.lambda]

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.

这很重要,因为一元运算符 + 有一组内置的重载,特别是这个:

This is important as the unary operator + has a set of built-in overloads, specifically this one:

8 对于每种类型T,都存在形式为

13.6 Built-in operators [over.built]

8 For every type T there exist candidate operator functions of the form

T* operator+(T*);

有了这个,很清楚会发生什么:当运算符 + 应用于闭包对象时,重载的内置候选集包含一个转换为任意指针和闭包类型只包含一个候选:转换为 lambda 的函数指针.

And with this, it's quite clear what happens: When operator + is applied to the closure object, the set of overloaded built-in candidates contains a conversion-to-any-pointer and the closure type contains exactly one candidate: The conversion to the function pointer of the lambda.

auto test = +[]{};test 的类型因此被推导出为void(*)().现在第二行很简单:对于第二个 lambda/closure 对象,对函数指针的赋值触发与第一行相同的转换.尽管第二个 lambda 表达式具有不同的闭包类型,但生成的函数指针当然是兼容的并且可以赋值.

The type of test in auto test = +[]{}; is therefore deduced to void(*)(). Now the second line is easy: For the second lambda/closure object, an assignment to the function pointer triggers the same conversion as in the first line. Even though the second lambda has a different closure type, the resulting function pointer is, of course, compatible and can be assigned.

这篇关于一个正 lambda:'+[]{}' - 这是什么魔法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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