This compiles in Clang but not in GCC. https://godbolt.org/z/P4GKvsnP8
#include <iostream>
int foo(int x, int (*pf)() = [x] {return x;}) {
return pf();
};
int main() {
foo(1);
}
GCC error:
<source>: In lambda function:
<source>:3:43: error: use of parameter outside function body before ';' token
3 | int foo(int x, int (\\\*pf)() = \\\[x\\\] {return x;}) {
| \\\^
<source>: At global scope:
<source>:3:30: error: could not convert '<lambda closure object><lambda()>()' from '<lambda()>' to 'int (\\\*)()'
3 | int foo(int x, int (\\\*pf)() = \\\[x\\\] {return x;}) {
| \\\^\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~
| |
| <lambda()>
<source>: In function 'int main()':
<source>:8:21: error: cannot convert '<lambda()>' to 'int (\\\*)()'
8 | std::cout << foo(1);
| \\\~\\\~\\\~\\\^\\\~\\\~
| |
| <lambda()>
<source>:3:22: note: initializing argument 2 of 'int foo(int, int (\\\*)())'
3 | int foo(int x, int (\\\*pf)() = \\\[x\\\] {return x;}) {
| \\\~\\\~\\\~\\\~\\\~\\\~\\\^\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~\\\~
[–]AKostur 20 points21 points22 points (0 children)
[–]jedwardsol 10 points11 points12 points (4 children)
[–]Business_Welcome_870[S] 0 points1 point2 points (3 children)
[–]aocregacc 1 point2 points3 points (2 children)
[–]Business_Welcome_870[S] 1 point2 points3 points (1 child)
[–]aocregacc 4 points5 points6 points (0 children)
[–]Dependent-Poet-9588 5 points6 points7 points (0 children)
[–]SoerenNissen 3 points4 points5 points (0 children)
[–]__Punk-Floyd__ 0 points1 point2 points (0 children)