无法从std :: bind推导std :: function的模板参数 [英] Could not deduce template argument for std::function from std::bind

查看:234
本文介绍了无法从std :: bind推导std :: function的模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来调用许多类成员函数,每个成员函数具有不同的参数,并且在调用之前和之后都会发生某些已知的功能。

I'm trying to find a way to call a number of class member functions, each having differing parameters, with certain known functionality happening before and after the call.

这个包装函数是我尝试过的,但例如,对该函数的最终调用不会编译错误:

This wrapper function is what I've tried, but for example the final call to it doesn't compile with error:


'bool包装器(Work *,std :: function< bool(Args ...)>,Args&& ...)':
不能推断
的模板参数'std :: function< bool(double,std :: string,Args ...)>’来自
’std :: _ Bind< true,bool,std :: _ Pmf_wrap<布尔(__thiscall Work :: *
)(double,std :: string),bool,Work,double,std :: string>,Work * const>'

'bool Wrapper(Work * ,std::function< bool(Args...)>,Args &&...)' : could not deduce template argument for 'std::function< bool(double,std::string,Args...)>' from 'std::_Bind< true,bool,std::_Pmf_wrap< bool (__thiscall Work::* )(double,std::string),bool,Work,double,std::string>,Work *const >'



class Work
    {
    public:
        void DoWork(int a, double b, string c);

    private:
        void Pre() {};
        void Post() {};
        bool Step1() { return true; }
        bool Step2(int) { return true; }
        bool Step3(double, string) { return true; }
    };

template<typename... Args>
bool Wrapper(Work *work, std::function<bool(Args...)> func, Args&&... args)
    {
    work->Pre();
    bool ret = func(std::forward<Args>(args)...);
    work->Post();
    return ret;
    }

void Work::DoWork(int a, double b, string c)
{
    if (!Wrapper<>(this, std::bind(&Work::Step1, this))) // error
        return;
    if (!Wrapper<int>(this, std::bind(&Work::Step2, this), a)) // error
        return;
    if (!Wrapper<double, string>(this, std::bind(&Work::Step3, this), b, c)) // error
        return;
}

int main()
{
    Work work;
    work.DoWork(1, 2.0, "three");
    return 0;
}

(乍看之下,将功能的前置和后置功能一眼看上去更可取,但这是不希望的,因为上面是实际代码的一个大大简化的示例,并且该步骤具有多个返回位置,并且没有测试。)

(Putting the pre- and post- functionality inside the steps would seem at first glance far preferable but that's undesirable as the above is a grossly simplified example of the actual code, and the steps have multiple places of returns, and no tests.)

显式模板参数将使模板解析成为可能。我在做什么错?

I thought the explicit template arguments would make the template resolution possible. What am I doing wrong?

推荐答案

使用C ++ 11, std :: bind 可以被lambda代替,您可以删除包装器模板:

With C++11, std::bind can be replaced by lambda and you can remove template for wrapper :

class Work
{
    public:
        void DoWork(int a, double b, string c);

    private:
        void Pre() {};
        void Post() {};
        bool Step1() { return true; }
        bool Step2(int) { return true; }
        bool Step3(double, string) { return true; }

        friend bool Wrapper(Work *work, std::function<bool()> func);
};

bool Wrapper(Work *work, std::function<bool()> func)
{
    work->Pre();
    bool ret = func();
    work->Post();
    return ret;
}

void Work::DoWork(int a, double b, string c)
{
    if (!Wrapper(this, [this]() -> bool { return this->Step1(); }))
        return;
    if (!Wrapper(this, [this, a]() -> bool { return this->Step2(a); }))
        return;
    if (!Wrapper(this, [this, b, c]() -> bool { return this->Step3(b, c); }))
        return;
}

int main()
{
    Work work;
    work.DoWork(1, 2.0, "three");
    return 0;
}

示例: http://coliru.stacked-crooked.com/a/2cd3b3e2a4abfcdc

这篇关于无法从std :: bind推导std :: function的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆