【问题标题】:Tbb library: error: no match for call to function when writing custom class function instead of lambda expressionTbb 库:错误:编写自定义类函数而不是 lambda 表达式时调用函数不匹配
【发布时间】:2020-01-05 13:25:57
【问题描述】:

我用“Pro TBB”一书学习线程构建块。

我想重写为类函数,而不是像作者写的那样使用 lambda 表达式。

这是本书的原始来源,经过我的测试,它确实有效:

#include <iostream>
#include <tbb/tbb.h>

int main()
{
    tbb::parallel_invoke(
        [](){std::cout<<"Hello "<<std::endl;},
        [](){std::cout<<"TBB! "<<std::endl;}
    );
    return 0;
}

但是当我写如下:

#include <iostream>
#include <tbb/tbb.h>

using std::cout;
using std::endl;

void print_function(const std::string &s)
{
    cout<<s<<endl;
}
class printClass
{
private:
    const std::string &myString;
public:
    printClass(const std::string &s): myString{s} {};
    void operator()(std::string s) const{
        print_function(myString);
    } 
};

int main()
{
    std::string s1 = "Hello", s2 = "TBB!";
    tbb::parallel_invoke(
        printClass(s1),
        printClass(s2)
    );
    return 0;
}

它产生了错误:

In file included from /usr/local/include/tbb/tbb.h:61:0,
                 from figure_1_04_class.cpp:2:
/usr/local/include/tbb/parallel_invoke.h: In instantiation of ‘tbb::task* tbb::internal::function_invoker<function>::execute() [with function = printClass]’:
figure_1_04_class.cpp:30:1:   required from here
/usr/local/include/tbb/parallel_invoke.h:47:24: error: no match for call to ‘(const printClass) ()’
             my_function();
                        ^
figure_1_04_class.cpp:17:10: note: candidate: void printClass::operator()(std::__cxx11::string) const
     void operator()(std::string s) const{
          ^
figure_1_04_class.cpp:17:10: note:   candidate expects 1 argument, 0 provided

我在上面的书第 2 章中效仿了他们的例子。 这是他们的例子,它也很有效:

#include <vector>
#include <tbb/tbb.h>
#include <iostream>

using std::cout;
using std::endl;

void print_fucntion(int v)
{
    cout<<"v: "<< v<<endl;    
}
void sidebar_pfor_lambda(int N, const std::vector<int> &a)
{
    tbb::parallel_for(0, N, 1, [&a](int i)
    {
        print_fucntion(a[i]);
    });
}

int main()
{
    std::vector<int> v = {4, 5, 6, 7, 8};
    sidebar_pfor_lambda(5, v);
    return 0;
}
#include <vector>
#include <tbb/tbb.h>
#include <iostream>

using std::cout;
using std::endl;

void print_fucntion(int v)
{
    cout<<"v: "<< v<<endl;    
}

class Body
{
private:
    const std::vector<int> &myVector;
public:
    Body(const std::vector<int> &v) : myVector{v} {};
    void operator()(int i) const {
        print_fucntion(myVector[i]);
    }
};

void sidebar_pfor_function(int N, const std::vector<int> &a)
{
    tbb::parallel_for(0, N, 1, Body(a));
}

int main()
{
    std::vector<int> v = {4, 5, 6, 7, 8};
    sidebar_pfor_function(5, v);
    return 0;
}

我做错了什么以及如何解决?

【问题讨论】:

    标签: c++ c++11 tbb


    【解决方案1】:

    tbb::parallel_invokeexpects 可以用零参数调用的函数对象:

    表达式parallel_invoke(f0,f1,...,fk) 可能并行计算f0()f1()、...、fk()

    您的第一个示例中的 Lambda 可以通过这种方式调用:

    auto l = [](){ std::cout << "Hello" << std::endl; };
    l();   // This is OK
    

    但是函数对象printClass 需要一个参数:

    std::string s1 = "Hello";
    auto l = printClass(s1);
    l();   // Not OK!
    

    编译器抱怨:

    候选人期望 1 个参数,提供 0 个参数

    解决方案非常简单——删除不必要的参数(它有什么用?):

    class printClass {
        // ...
        void operator()(/* std::string s */) const {
            print_function(myString);
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-23
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2019-03-05
      • 1970-01-01
      相关资源
      最近更新 更多