7

Consider the following code:

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

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;
}

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?


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.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • 1
    The GCC compiler (5.1.0) tells you it's unimplemented: " sorry, unimplemented: converting lambda which uses '...' to function pointer" –  Dec 24 '16 at 22:00
  • @latedeveloper Ahahah... I tried it on mobile and the error message was shorter (probably cut out) while godbolt on mobile is far from being user-friendly. Good catch, didn't see it. So, I won't open any issue, they already know it!! Thank you. I'm going to close my own question. – skypjack Dec 24 '16 at 22:06
  • 3
    The error message is pretty clear, no actual question here. – skypjack Dec 24 '16 at 22:08
  • You just wanted a hat for voting to close a question you yourself asked, right? – David Hammen Dec 24 '16 at 22:46
  • @DavidHammen Honestly I don't even know if that kind of hat exists. I'm not that interested in those things. Do you prefer me to delete the question? I'd simply leave it for future readers and this makes much more sense than a hat from my point of view. – skypjack Dec 24 '16 at 22:51
  • Just joking, @skypjack. Happy holidays! That would be a very rare hat, if it existed. – David Hammen Dec 24 '16 at 22:56
  • @DavidHammen :-D ... It sounded strange actually, but there exist badges that have no sense for me too. Therefore such a hat would have been perfectly legal somehow!! :-) – skypjack Dec 24 '16 at 23:05

1 Answers1

11

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).

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.

T.C.
  • 133,968
  • 17
  • 288
  • 421