C++ 我不能将 lambda 作为 std::function 传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36030589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:34:31  来源:igfitidea点击:

I cannot pass lambda as std::function

c++c++11lambda

提问by Gilgamesz

Let's focus on this example:

让我们关注这个例子:

template<typename T>
class C{
    public:
    void func(std::vector<T>& vec, std::function<T( const std::string)>& f){
        //Do Something
    }
};

And now, I am trying:

现在,我正在尝试:

std::vector<int> vec;
auto lambda = [](const std::string& s) { return std::stoi(s); };
C<int> c;
c.func(vec, lambda);

It causes errors:

它会导致错误:

no matching function for call to ‘C<int>::func(std::vector<int, std::allocator<int> >&, main()::<lambda(const string&)>&)'
     ref.parse(vec, lambda);

Please explain me what is not ok and how to implement it with std::bind as well.

请向我解释什么是不好的,以及如何用 std::bind 实现它。

采纳答案by Jens

It's because a lambda function is not a std::function<...>. The type of

这是因为 lambda 函数不是std::function<...>. 的类型

auto lambda = [](const std::string& s) { return std::stoi(s); };

is not std::function<int(const std::string&)>, but something unspecified which can be assigned to a std::function. Now, when you call your method, the compiler complains that the types don't match, as conversion would mean to create a temporary which cannot bind to a non-const reference.

不是std::function<int(const std::string&)>,而是可以分配给 a 的未指定的东西std::function。现在,当您调用您的方法时,编译器会抱怨类型不匹配,因为转换意味着创建一个无法绑定到非常量引用的临时对象。

This is also not specific to lambda functions as the error happens when you pass a normal function. This won't work either:

这也不是特定于 lambda 函数,因为当您传递普通函数时会发生错误。这也行不通:

int f(std::string const&) {return 0;}

int main()
{
    std::vector<int> vec;
    C<int> c;
    c.func(vec, f);
}

You can either assign the lambda to a std::function

您可以将 lambda 分配给 std::function

std::function<int(const std::string&)> lambda = [](const std::string& s) { return std::stoi(s); };

,change your member-function to take the function by value or const-reference or make the function parameter a template type. This will be slightly more efficient in case you pass a lambda or normal function pointer, but I personally like the expressive std::functiontype in the signature.

, 更改您的成员函数以按值或常量引用获取函数或使函数参数成为模板类型。如果您传递 lambda 或普通函数指针,这将稍微更有效,但我个人喜欢std::function签名中的表达类型。

template<typename T>
class C{
    public:
    void func(std::vector<T>& vec, std::function<T( const std::string)> f){
        //Do Something
    }

    // or
    void func(std::vector<T>& vec, std::function<T( const std::string)> const& f){
        //Do Something
    }

    // or
    template<typename F> func(std::vector<T>& vec, F f){
        //Do Something
    }
};

回答by bolov

It's because the argument (std::function) is a reference. It should be:

这是因为参数 ( std::function) 是一个引用。它应该是:

void func(std::vector<T>& vec, std::function<T(const std::string&)> f)
                                                                ^  ^
                                                                   |
                                                                   f not a reference

So that the argument can be converted to the parameter type.

这样参数就可以转换为参数类型。

Also, the type of the function should match. I.e. it should accept a string reference.

此外,函数的类型应该匹配。即它应该接受一个字符串引用。