C++ std::使用变量函数参数绑定,包括使用变量参数的回调函数

C++ std::使用变量函数参数绑定,包括使用变量参数的回调函数,c++,c++11,variadic-functions,stdbind,C++,C++11,Variadic Functions,Stdbind,如果我有一个带有变量参数的函数,其中一个是回调函数,那么bind函数如何工作呢? 目前的执行情况如下: template <typename... Args> bool CallWithArgs(std::function<void (String&, Args... args)> cbk, Args... args) { .... } 模板 bool CallWithArgs(标准::函数cbk,Args…Args) { .... } 使用future从单

如果我有一个带有变量参数的函数,其中一个是回调函数,那么bind函数如何工作呢? 目前的执行情况如下:

template <typename... Args>
bool CallWithArgs(std::function<void (String&, Args... args)> cbk, Args... args) 
{ .... }
模板
bool CallWithArgs(标准::函数cbk,Args…Args)
{ .... }
使用future从单独的类调用上述函数:

bool value = true;
auto f1 = std::bind(&CallWithArgs, rawPtr, _1, _2);
std::future<bool> fut = std::async(f1, cbk, value);
fut.wait();
bool值=真;
auto f1=std::bind(&CallWithArgs,rawPtr,_1,_2);
std::future fut=std::async(f1,cbk,value);
等等;
在std::bind函数的占位符中是否有表示变量参数的方法?在当前实现中遇到编译问题

note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)

note:   template argument deduction/substitution failed:
note:   couldn't deduce template parameter ‘_Func’
注意:模板typename std::_Bind_helper::type std::Bind(_Func&,_BoundArgs&&…)
绑定(_Func&&u f,_BoundArgs&&…_参数)
注意:模板参数扣除/替换失败:
注意:无法推断模板参数“\u Func”

由于不能使用C++14的通用lambda,因此可以通过生成函子来创建自己的lambda。如果你有

struct CallWithArgsFunctor
{
    pointer_type pointer_to_call_on;
    CallWithArgsFunctor(pointer_type pointer_to_call_on) : pointer_to_call_on(pointer_to_call_on) {}
    template<typename... Args>
    auto operator()(Args&&... args) -> decltype(CallWithArgs(pointer_to_call_on, std::forward<Args>(args)...))
    {
        return CallWithArgs(pointer_to_call_on, std::forward<Args>(args)...)
    }
};
struct CallWithArgsFunctor
{
指针类型指向调用的指针;
CallWithArgsFunctor(指针类型指针到调用上):指针到调用上(指针到调用上){
样板
自动运算符()(Args&&…Args)->decltype(CallWithArgs(指向调用的指针,std::forward(Args)…)
{
return CallWithArgs(指向调用的指针,std::forward(args)…)
}
};
然后可以在代码块中使用它,如

bool value = true;
std::future<bool> fut = std::async(CallWithArgsFunctor{rawPtr}, cbk, value);
fut.wait();
bool值=真;
std::future fut=std::async(CallWithArgsFunctor{rawPtr},cbk,value);
等等;
这允许重载解析在调用操作符的主体中工作,而不必将函数指针强制转换为要调用的类型


如果你能升级到C++14,你的代码就会变得非常简单

bool value = true;
auto f1 = [=](auto&&... args){ return CallWithArgs(rawPtr, std::forward<decltype(args)>(args)...); };
std::future<bool> fut = std::async(f1, cbk, value);
fut.wait();
bool值=真;
自动f1=[=](自动和…参数){返回CallWithArgs(rawPtr,std::forward(参数)…);};
std::future fut=std::async(f1,cbk,value);
等等;

您是否考虑过改用lambda?(仔细想想:您可能需要C++14)这是一个纯粹的学术问题,还是您正在尝试让某些东西工作?如果是后者,答案很简单:只需使用lambda就可以了。我正在努力让它工作。使用lambda的唯一问题是,带有变量args的函数是公共库的一部分。因此,将其设置为lambda并将其放在我调用它的类中是不可行的。@PK17我不清楚为什么将lambda放在类中不如
std::bind
@eerorika可行,因为我提到了CallWithArgs fn是公共库的一部分,需要访问该公共库中的其他函数/变量。我计划进一步支持从其他组件/可执行文件调用该函数。不希望每个组件都实现相同的fn CallWithArgs作为lambda函数。升级到c++14会很好(是:),但不幸的是,目前不可能。将尝试使用函子的建议,并在此处更新。谢谢@PK17没问题。让我知道它是否适合你。谢谢@NathanOliver你的建议奏效了。还创建了一个单独的测试文件来尝试C++14方式,这也很有效@PK17真棒。很高兴它对你有用。希望有一天你可以升级:)