Use a lambda as a parameter for a C++ function
Solution 1
You have 2 ways: make your function template:
template <typename F>
void myFunction(F&& lambda)
{
//some things
}
or erase type (with std::function
for example):
void
myFunction(const std::function<void()/*type of your lamdba::operator()*/>& f)
{
//some things
}
Solution 2
You have two choices, basically.
Make it a template:
template<typename T>
void myFunction(T&& lambda){
}
or, if you do not want (or can't) do that, you can use type-erased std::function
:
void myFunction(std::function<void()> const& lambda){
}
Conversely, your attempt with auto
would've been correct under the concepts TS as currently implemented in gcc, where it'd be an abbreviated template.
// hypothetical C++2x code
void myFunction(auto&& lambda){
}
or with a concept:
// hypothetical C++2x code
void myFunction(Callable&& lambda){
}
Solution 3
If this is an inline
function, prefer a template, as in
template<typename Func>
void myFunction(Func const&lambda)
{
//some things
}
because it binds to anything that makes sense (and will cause compiler error for anything else), including lambdas, instances of named classes, and std::function<>
objects.
On the other hand, if this function is not inline
, i.e. implemented in some compilation unit, you cannot use a generic template but must use a specified type, which is best taken a std::function<>
object and passed via reference.
Solution 4
Pass it as you'd pass a simple function. Just give it a name with auto
#include <iostream>
int SimpleFunc(int x) { return x + 100; }
int UsingFunc(int x, int(*ptr)(int)) { return ptr(x); }
auto lambda = [](int jo) { return jo + 10; };
int main() {
std::cout << "Simple function passed by a pointer: " << UsingFunc(5, SimpleFunc) << std::endl;
std::cout << "Lambda function passed by a pointer: " << UsingFunc(5, lambda) << std::endl;
}
Output:
Simple function passed by a pointer: 105
Lambda function passed by a pointer: 15
Donald Duck
Please try Map Collector, an online game that I've created. If you have any suggestions for how I could make it better, there is a contact link in the footer. Please don't use code formatting on random keywords. If you do, I suggest you read Should I use code blocks when mentioning framework names/technologies? and Inline Code Spans should not be used for emphasis, right? If you want to help answering questions that haven't gotten enough attention, you can use this query to find such questions. It lets you filter questions by tag to help you find questions you know how to answer. Here are some Meta posts that I particularly agree with: Please put back the "last seen" and "member since" metrics Can we allow 7.5K users to suggest tag synonyms without a score of 5 in the tag? (this answer in particular) The title word filter is one of the worst ideas ever implemented on SO Review audits and "I understand" button Should I use code blocks when mentioning framework names/technologies? Show all of my question/answers to me even if they are deleted
Updated on July 21, 2022Comments
-
Donald Duck almost 2 years
I would like to use a lambda as a parameter for a C++ function, but I don't know which type to specify in the function declaration. What I would like to do is this:
void myFunction(WhatToPutHere lambda){ //some things }
I have tried
void myFunction(auto lambda)
andvoid myFunction(void lambda)
but none of these codes compiled. In case it matters, the lambda doesn't return anything.How can I use a lambda as a parameter in a C++ function?