Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I convert [](auto&&...){} into a std::function<void()>?

Tags:

c++

gcc

lambda

When I try to compile this:

#include <functional>

void f(std::function<void()> f)
{
}

void g()
{
  f([](auto&&...){});
}

on gcc 7.3, I get the following error:

[x86-64 gcc 7.3 #1] error: could not convert '<lambda closure object>g()::<lambda(auto:1&&, ...)>{}' from 'g()::<lambda(auto:1&&, ...)>' to 'std::function<void()>'

Can someone explain why this is invalid c++? Or should I submit a bug report? (MSVC 14 accepts and compiles it to what I expect.)

like image 923
B Simpson Avatar asked Feb 05 '18 17:02

B Simpson


People also ask

How c++ auto works?

The auto keyword in C++ automatically detects and assigns a data type to the variable with which it is used. The compiler analyses the variable's data type by looking at its initialization. It is necessary to initialize the variable when declaring it using the auto keyword.

How do you pass lambda function in CPP?

Permalink. All the alternatives to passing a lambda by value actually capture a lambda's address, be it by const l-value reference, by non-const l-value reference, by universal reference, or by pointer.

Is a C++ lambda a function pointer?

However, a capture-less lambda has an additional function-pointer conversion operator declared in it. It is that function-pointer conversion operator that makes a capture-less lambda convertible to a function pointer.


1 Answers

This is a gcc bug. It interprets your lambda as follow:

[](auto&&, ...){}

So there's one argument, followed by C variadic.

If you add a name to your parameter pack, then it works as intended:

[](auto&&... pack){}
like image 168
Guillaume Racicot Avatar answered Sep 16 '22 14:09

Guillaume Racicot