【发布时间】:2018-11-05 21:26:13
【问题描述】:
我有以下代码使用可变参数模板调用std::async,
struct TestParent
{
template< typename Fn, typeName ...Args >
bool checkBlock( bool& toCheck,
Fn&& fn, Args&& ... args )
{
int startIndx = 0;
int endIndx = 10;
std::future< bool > tk( std::async( std::launch, fn, this,
startIndx, endIndx,
toCheck, args ... ) );
return tk.get();
}
}
struct TestChild: public TestParent
{
bool checkRules( const int, const int, bool& std::vector< bool >& );
bool check();
}
bool TestChild::checkRules( const int startIndx, const int endIndx,
bool& toCheck,
std::vector< bool >& results )
{
toCheck = true;
for ( int indx = startIndx; indx < endIndx; ++ indx )
{
bool checkedOk;
... do something checking.
results.push_back( checkedOk );
}
return true;
}
bool TestChild::check()
{
bool toCheck;
std::vector< bool > results;
return checkBlock( toCheck, &testChild::checkRules, this, &results);
}
但我收到以下编译错误消息:
没有匹配函数调用 'async(std::launch, bool (TestChild::&)(int, int, bool&, std::vector&), TestParent, int&, int&, bool&, TestChild*&, std::vector*&)' startInx, endInx, nothingToCheck, args ... ) );
我认为这可能与我将附加参数与参数包一起传递的事实有关。 任何人都知道这有什么问题,我应该怎么做才能让它发挥作用?
【问题讨论】:
-
在这行代码中:
template< typename Fn, typeName ...Args >第二个参数typeName是错字吗?同样在这行代码中:bool checkRules( const int, const int, bool& std::vector< bool >& );您是否在参数之间缺少逗号? -
是的,弗朗西斯。当我在这里输入时,我错过了那个逗号。
-
我以为你做了,但想确定一下,因为像这样简单的事情会产生与你描述的完全不同的错误。这就是为什么最好尽可能将代码从 IDE 直接复制到堆栈帖子。
标签: c++ variadic-templates stdasync