C++ C++;可变模板误差

C++ C++;可变模板误差,c++,variadic-templates,stdasync,C++,Variadic Templates,Stdasync,下面的代码使用可变模板调用std::async struct TestParent { template< typename Fn, typeName ...Args > bool checkBlock( bool& toCheck, Fn&& fn, Args&& ... args ) { int startIndx = 0; int endIn

下面的代码使用可变模板调用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);
}
struct TestParent
{
模板
bool检查块(bool&toCheck,
Fn&&Fn,Args&&Args)
{
int startIndx=0;
int-endIndx=10;
std::futuretk(std::async(std::launch,fn,this,
startIndx,endIndx,
toCheck,args…);
返回tk.get();
}
}
结构TestChild:公共TestParent
{
布尔检查规则(常量int、常量int、布尔和标准::向量&);
布尔检查();
}
bool TestChild::checkRules(常量int startIndx、常量int endIndx、,
布尔和托切克,
标准::向量&结果)
{
toCheck=true;
对于(int indx=startIndx;indx结果;
返回检查块(toCheck,&testChild::checkRules,this,&results);
}
但我得到以下编译错误消息:

调用“异步(std::launch,bool(TestChild::&)(int,int,bool&,std::vector&),TestParent,int&,int&,bool&,TestChild*&,std::vector*&)”时没有匹配的函数 startInx、endInx、nothingToCheck、args…)

我认为这可能与我在传递参数包的同时传递其他参数这一事实有关。 有人知道这有什么问题吗?我该怎么做才能让它发挥作用

return checkBlock( toCheck, &testChild::checkRules, this, &results);
您将
这个
与您的
参数
一起传递,该参数与您的函数不匹配,因此有一个额外的
TestChild*&
不属于您

return checkBlock( toCheck, &testChild::checkRules, ~~this~~, &results);
把这个拿走~~

此外,您还应该std::转发
Args
,如下所示:

                                        toCheck, std::forward<Args>(args) ... ) );
toCheck,std::forward(args)…);

这些是代码中的两个主要问题:

(1)
std::async
先衰减所有传递的参数,然后再将其转发给提供的函数,这意味着
checkRules
中的references参数不同于调用函数时尝试使用的类型
async
,您需要做以下更改:

template< typename Fn, typename ...Args >
bool checkBlock( std::reference_wrapper<bool> const& toCheck,
                Fn&& fn, Args&& ... args )
{
    int startIndx = 0;
    int endIndx = 10;
    std::future< bool > tk(std::async(std::launch::async,
                                       std::forward<Fn>(fn),
                                       startIndx, endIndx,
                                       toCheck,
                                       std::forward<Args>(args) ... ) );
    return tk.get();
}

在这一行代码中:
template
第二个参数
typename
是打字错误吗?同样在这行代码中:
bool检查规则(const int,const int,bool&std::vector&)参数之间是否缺少逗号?是的,Francis。当我在这里输入时,我漏掉了那个逗号。我以为你漏掉了,但我想确认一下,因为像这样简单的事情可能会产生与你描述的完全不同的错误。这就是为什么尽可能将代码从IDE直接复制到堆栈帖子中的原因。哇,回答得太好了!我错过了解决这个问题的两个主要部分,而你抢先解决了。由于
衰变
std::ref
一起使用
std::ref
,我错过了使用
std::reference\u包装器的事实,我可能最终会转移到
。我很感激这种回答,因为我自己学到了一些新东西!我认为你应该得到公认的答案!非常感谢你,简!这无疑是向前迈出了一大步,但不幸的是还没有达到这一步:-(我现在收到一些错误,抱怨“没有名为'type'的类型”如下:functional:1665:61:error:class std::result\u of'typedef typename result\u of::type result\u type中没有名为'type'的类型;谢谢!谢谢你,Francis!我还将仔细查看std::placeholder::\u 1…我自己,这对我来说也是新的。@mark,你仔细查看了答案中的代码了吗这些更改使错误消失。@Jans:非常感谢Jans!是的,添加引用包装器使错误消失,现在它编译了~非常感谢您的帮助!!谢谢您Xaxxon!我尝试删除“this”并将std::forward提交给Args和Fn,但仍然无效:-(
#include <functional>
using namespace std::placeholders;

bool TestChild::check()
{
    bool toCheck;
    std::vector< bool > results;
    return checkBlock( std::ref(toCheck), std::bind(&TestChild::checkRules, this, _1, _2, _3, _4), std::ref(results));
}