非捕获通用lambda是否应该衰减为函数指针? [英] Should non-capturing generic lambdas decay to function pointers?

查看:76
本文介绍了非捕获通用lambda是否应该衰减为函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

int main() {
    auto l = [](auto){};
    void(*p)(int) = l;
}

对于 GCC clang

让我们考虑以下经过稍微修改的版本:

It works just fine both with GCC and clang.
Let's consider the following slightly modified version:

int main() {
    auto l = [](auto...){};
    void(*p)(int) = l;
}

在这种情况下,clang仍然接受,而GCC 拒绝

In this case, clang still accepts it while GCC rejects it.

是否有任何理由应拒绝此代码,或者它是编译器的错误?

Is there any reason for which this code should be rejected or is it a bug of the compiler?

我要提出一个问题,但我想知道是否存在任何一项提案可以由其中一个提案而不是由另一个提案实施。 / p>

I'm going to open an issue, but I'd like to know if there exists any proposal that could have been implemented by one of them and not by the other one.

推荐答案

这是一个已知的GCC解析错误( 64095 68071 ): [](auto ...){} 被错误地解析为 [](auto,.. 。){} ,而不是 [](auto ... x){} ;省略号将被解析为C样式的变量,而不是声明一个参数包(以语言律师的说法,它被解析为 parameter-declaration-clause 的一部分,而不是 abstract-声明者,违反了 [dcl.fct] / 17 )。

This is a known GCC parsing bug (64095, 68071): [](auto...){} is being mistakenly parsed like [](auto, ...) {} rather than [](auto...x){}; the ellipsis is being parsed as C-style varargs rather than declaring a parameter pack (in language-lawyer terms, it's being parsed as part of the parameter-declaration-clause rather than the abstract-declarator, in violation of [dcl.fct]/17).

不应该说 [](auto,...){} 't可转换为 void(*)(int)

It should go without saying that [](auto, ...){} isn't convertible to void (*)(int).

解决方法是为包装命名。如果您这样做,将会看到转换成功编译。

The workaround is to give the pack a name; if you do, you'll see that the conversion compiles successfully.

这篇关于非捕获通用lambda是否应该衰减为函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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