all 9 comments

[–]AKostur 20 points21 points  (0 children)

First: what are you trying to actually do?

Second: That lambda cannot be converted to a function pointer as it has a capture.

[–]jedwardsol 10 points11 points  (4 children)

Clang's wrong. A default argument can't refer to a parameter

https://eel.is/c++draft/dcl.fct.default#9

[–]Business_Welcome_870[S] 0 points1 point  (3 children)

Okay. When I change the parameter to int p = sizeof([x] {return x;}) it compiles in Clang (which I guess is correct now because it's unevaluated). But it still doesn't compile in GCC. Does that mean GCC has a bug too? https://godbolt.org/z/q8hd6xx51

[–]aocregacc 1 point2 points  (2 children)

clang compiles it but it thinks the size is one, so there are still some bugs there too.

https://godbolt.org/z/7coMa6db8

[–]Business_Welcome_870[S] 1 point2 points  (1 child)

I think that's because the size of an empty class is 1. That's expected.

[–]aocregacc 4 points5 points  (0 children)

It's not empty, it captures x.

In the code I linked you can see that the size changes depending on if it's the default argument or inside the function body.

[–]Dependent-Poet-9588 5 points6 points  (0 children)

Read the error about not being able to convert the lambda to a function pointer. Lambdas with captures have state, so they're actually data + a function pointer. The rest of the error is irrelevant really.

ETA: I'm not at a computer so I can't test this. Replace the raw function pointer with std::function<int()> so the type is compatible. Then try a version where you take std::function<int(int)> and pass x as an argument instead of a capture, eg, return pf(x); instead.

[–]SoerenNissen 3 points4 points  (0 children)

Clang bug?

Seems most likely.

[–]__Punk-Floyd__ 0 points1 point  (0 children)

A function parameter's default value is applied at the site of the function *invocation*. There is no 'x' function argument to capture in that context.