Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "lambda is not derived from 'std::function'

I'm trying to pass lambda to std::function<> defined via variadic template, but it seems that this doesn't work on gcc.

Is there any reason, why this code doesn't work on gcc 7.4.0 but working correctly on Visual Studio 2017? And is there any way how to make it work also on gcc without the necessity to manually convert it to std::function<> first?

#include <functional>

template<class ...TParams>
int TestFunction(std::function<void(TParams...)> )
{
    return 0;
}

void Test()
{
    auto fce = [](int /*n*/, double /*d*/) {};

    //This doesn't work with error no matching function for call to 'TestFunction<int, double>(Test()::<lambda(int, double)>&)'
    TestFunction<int, double>(fce);

    //but this works correctly
    std::function<void(int, double)> fce2 = fce;
    TestFunction<int, double>(fce2);
}

I'm getting following error:

main.cpp: In function 'void Test()':
main.cpp:116:31: error: no matching function for call to 'TestFunction<int, double>(Test()::<lambda(int, double)>&)'
  TestFunction<int, double>(fce);
                               ^
main.cpp:106:5: note: candidate: template<class ... TParams> int TestFunction(std::function<void(TParams ...)>)
 int TestFunction(std::function<void(TParams...)> fceCallback)
     ^~~~~~~~~~~~
main.cpp:106:5: note:   template argument deduction/substitution failed:
main.cpp:116:31: note:   'Test()::<lambda(int, double)>' is not derived from 'std::function<void(TParams ...)>'
  TestFunction<int, double>(fce);
                               ^
like image 583
Ludek Vodicka Avatar asked Apr 14 '19 13:04

Ludek Vodicka


People also ask

How to create a lambda function in C++?

Creating a Lambda Expression in C++auto greet = []() { // lambda function body }; Here, [] is called the lambda introducer which denotes the start of the lambda expression. () is called the parameter list which is similar to the () operator of a normal function.

How lambda function works in C++?

Mutable specification Typically, a lambda's function call operator is const-by-value, but use of the mutable keyword cancels this out. It doesn't produce mutable data members. The mutable specification enables the body of a lambda expression to modify variables that are captured by value.

What does [&] mean in lambda in C++?

[&] means capturing all of them by reference, and [=] would mean to capture all of them by value. (If you use [&] , note that the lambda body will use the value of the variable at the time the lambda is executed, and not the value which was valid when you created the lambda!

What is lambda capture C++?

A capture clause of lambda definition is used to specify which variables are captured and whether they are captured by reference or by value. An empty capture closure [ ], indicates that no variables are used by lambda which means it can only access variables that are local to it.


1 Answers

A trailing template parameter pack always leaves room for further deduction. Specifying the first two arguments doesn't prevent you from doing something like this:

std::function<void(int, double, char)> fce3 ;
TestFunction<int, double>(fce3);

In this case, the pack will contain int, double, char, because the char was deduced from the function argument. Now, because deduction isn't over, and a lambda is not a std::function, the substitution fails.

To make this work, you need to let the deduction process know it's over, that an instantiated function is needed now, before it's given an argument. One way to do that is to take the function's address, for instance:

auto pfunc = TestFunction<int, double>;
pfunc(fce);

or

(&TestFunction<int, double>)(fce);

Taking a function template's address is another context where template argument deduction can occur. In this case, the trailing pack is deduced as empty, and you get a pointer to a function you may call.

like image 159
StoryTeller - Unslander Monica Avatar answered Nov 10 '22 12:11

StoryTeller - Unslander Monica