简体   繁体   English

使用 lambda 作为 C++ 函数的参数

[英]Use a lambda as a parameter for a C++ function

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.我想使用 lambda 作为 C++ 函数的参数,但我不知道在函数声明中指定哪种类型。 What I would like to do is this:我想做的是:

void myFunction(WhatToPutHere lambda){
    //some things
}

I have tried void myFunction(auto lambda) and void myFunction(void lambda) but none of these codes compiled.我试过void myFunction(auto lambda)void myFunction(void lambda)但这些代码都没有编译。 In case it matters, the lambda doesn't return anything.如果重要,lambda 不会返回任何内容。

How can I use a lambda as a parameter in a C++ function?如何在 C++ 函数中使用 lambda 作为参数?

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):或擦除类型(例如使用std::function ):

void
myFunction(const std::function<void()/*type of your lamdba::operator()*/>& f)
{
    //some things
}

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 :或者,如果您不想(或不能)这样做,您可以使用类型擦除的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 .相反,在当前在 gcc 中实现的概念 TS 下,您对auto的尝试是正确的,它是一个缩写的 template

// hypothetical C++2x code
void myFunction(auto&& lambda){
}

or with a concept:或有一个概念:

// hypothetical C++2x code
void myFunction(Callable&& lambda){
}

If this is an inline function, prefer a template, as in如果这是一个inline函数,则更喜欢模板,如

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.因为它绑定到任何有意义的东西(并且会导致其他任何东西的编译器错误),包括 lambda、命名类的实例和std::function<>对象。

On the other hand, if this function is not inline , ie 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.另一方面,如果此函数不是inline ,即在某些编译单元中实现,则不能使用通用模板,而必须使用指定类型,最好采用std::function<>对象并通过引用传递。

Pass it as you'd pass a simple function.像传递一个简单的函数一样传递它。 Just give it a name with auto给它一个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指针传递的简单函数:105
Lambda function passed by a pointer: 15通过指针传递的 Lambda 函数:15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM