0

Why does the compiler deny conversion of variadic lamda to function pointer? The conversion works perfectly fine without the variadic args.

    auto lambda = [] (void *, const char *) {};
    auto variadicLambda = [] (void *, const char *, auto ...) {};

    auto ptrLambda = +[] (void *, const char *) {};
    auto ptrVariadicLambda = +[] (void *, const char *, auto ...) {};

Tried with gcc 12.2 and clang 15.0.0.

<source>:7:30: error: invalid argument type '(lambda at <source>:7:31)' to unary expression
    auto ptrVariadicLambda = +[] (void *, const char *, auto ...) {};
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

https://godbolt.org/z/G6cnK6oxP

3
  • What function pointer type would it give? Commented Nov 23, 2022 at 14:25
  • 1
    Using auto creates a generic lambda, which is a templated functor and templates are not objects. Commented Nov 23, 2022 at 14:45
  • because (void *, const char *, int) and (void *, const char *, float) are different function and cannot squeeze into same pointer. (and what's the pointer type is another problem) Commented Nov 23, 2022 at 15:03

1 Answer 1

2

Why does the compiler deny conversion of variadic lamda to function pointer?

Because there is no overloaded operator+ that takes a generic lambda as its argument. This is because the use of auto in the parameter of the lambda makes it a generic lambda but since there is no operator+ for a generic lambda we get the mentioned error. This is exactly what the error says:

no match for 'operator+' (operand type is '<lambda(void*, const char*, auto:17 ...)>')
   12 | auto ptrVariadicLambda = +[] (void *, const char *, auto ...) {};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.