Tbb library: error: no match for call to function when writing custom class function instead of lambda expression
Tbb library: error: no match for call to function when writing custom class function instead of lambda expression
我正在学习“Pro TBB”这本书的 Thread Building Block。
我想重写 class 函数而不是像作者写的那样使用 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;
}
我做错了什么以及如何解决?
tbb::parallel_invoke
expects 可以用零参数调用的函数对象:
The expression parallel_invoke(f0,f1,...,fk)
evaluates f0()
, f1()
, ..., fk()
possibly in parallel.
第一个示例中的 Lambda 可以这样调用:
auto l = [](){ std::cout << "Hello" << std::endl; };
l(); // This is OK
但是函数对象 printClass
需要一个参数:
std::string s1 = "Hello";
auto l = printClass(s1);
l(); // Not OK!
编译器抱怨:
candidate expects 1 argument, 0 provided
解决方案非常简单——删除不必要的参数(它有什么用?):
class printClass {
// ...
void operator()(/* std::string s */) const {
print_function(myString);
}
};
我正在学习“Pro TBB”这本书的 Thread Building Block。
我想重写 class 函数而不是像作者写的那样使用 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;
}
我做错了什么以及如何解决?
tbb::parallel_invoke
expects 可以用零参数调用的函数对象:
The expression
parallel_invoke(f0,f1,...,fk)
evaluatesf0()
,f1()
, ...,fk()
possibly in parallel.
第一个示例中的 Lambda 可以这样调用:
auto l = [](){ std::cout << "Hello" << std::endl; };
l(); // This is OK
但是函数对象 printClass
需要一个参数:
std::string s1 = "Hello";
auto l = printClass(s1);
l(); // Not OK!
编译器抱怨:
candidate expects 1 argument, 0 provided
解决方案非常简单——删除不必要的参数(它有什么用?):
class printClass {
// ...
void operator()(/* std::string s */) const {
print_function(myString);
}
};