C ++:获取std :: bind产生的参数

时间:2012-02-21 17:42:43

标签: c++ c++11 variadic-templates std-function stdbind

首先,一点背景:在我的工作中,我们绑定了稍后调用的回调,这可能会使得尝试跟踪日志中的控制流非常困难。为了解决这个问题,我们使用“日志上下文”,它允许您在请求通过系统时跟踪请求。您可以使用静态函数log_context::get_current复制当前上下文,并使用静态函数log_context::set_current将其恢复。每次我们将回调发布到工作队列时,这都会导致大量重复的代码。

我想创建一个函数,它是std::bind的替代品,可以保存当前log_context并在调用时恢复它。但是,我在写它时遇到了一些麻烦。

现在,该功能如下所示:

template <typename TResult,
          typename... TFuncArgs,
          typename Func,
          typename... TProvidedArgs
         >
std::function<TResult (TFuncArgs...)>
bind_with_context(const std::function<TResult (TFuncArgs...)>& throwaway,
                  Func func,
                  TProvidedArgs&&... args
                 )
{
    log_context cxt = log_context::get_current();
    auto bound = std::bind(func, std::forward<TProvidedArgs>(args)...);
    auto lambda = [cxt, bound] (TFuncArgs... args) -> TResult
                  {
                      log_context::set_current(cxt);
                      return bound(args...);
                  };
    return lambda;
}

它有效,但问题是用法要求你传递函数类型没有真正的原因(除了我是如何找到用于TFuncArgs的内容):

bind_with_context(func_type(), &some_class::some_func, ptr, _1, "Bob", _2);

所以,不是一个简单的替代品。似乎一个应该在编译时知道这些信息,我只是无法弄清楚如何。它几乎就在那里。 如何消除传递函数类型的需要?


我最初的想法是将绑定分割为将其转换为如此的函数:

template <typename Func>
struct context_binder
{
public:
    context_binder(const Func& func) :
            func(func)
    { }

    // Use the casting operator to figure out what we're looking for:
    template <typename TReturn, typename... TFuncArgs>
    operator std::function<TReturn (TFuncArgs...)>() const
    {
        log_context cxt = log_context::get_current();
        auto lambda = [func, cxt] (TFuncArgs... args) -> TReturn
                      {
                          log_context::set_current(cxt);
                          return func(std::forward<TFuncArgs>(args)...);
                      };
        return lambda;
    }

private:
    Func func;
};

template <typename F, typename... TArgs>
auto bind_with_context(F f, TArgs&&... args)
        -> context_binder<decltype(std::bind(f, std::forward<TArgs>(args)...))>
{
    return std::bind(f, std::forward<TArgs>(args)...);
}

问题在于,永远不会调用强制转换(operator std::function<TReturn (TFuncArgs...)>() const)(给定int foo(int x, int y, int z)):

std::function<int (int)> f = bind_with_context(&foo, 4, 5, _1);

原因是function的构造函数试图从operator ()抓取context_binder(即使它没有){。

In file included from scratch.cpp:1:0:
/usr/local/include/gcc-4.6.2/functional: In static member function ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = int, _Functor = context_binder<std::_Bind<int (*(int, int, std::_Placeholder<1>))(int, int, int)> >, _ArgTypes = {int}]’:
/usr/local/include/gcc-4.6.2/functional:2148:6:   instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = context_binder<std::_Bind<int (*(int, int, std::_Placeholder<1>))(int, int, int)> >, _Res = int, _ArgTypes = {int}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<int(int)>::_Useless]’
scratch.cpp:53:85:   instantiated from here
/usr/local/include/gcc-4.6.2/functional:1764:40: error: no match for call to ‘(context_binder<std::_Bind<int (*(int, int, std::_Placeholder<1>))(int, int, int)> >) (int)’

所以我对这个几乎解决方案的问题是:有没有办法让g++更喜欢我的强制转换运算符而不是尝试使用function的构造函数?

1 个答案:

答案 0 :(得分:3)

解决方案是使用std::function将绑定与转换分离为operator ()

template <typename Func>
struct context_binder
{
private:
    Func        func;
    log_context cxt;

public:
    context_binder(const Func& func) :
            func(func),
            cxt(log_context::get_current())
    { }

    template <typename... TArgs>
    auto operator ()(TArgs&&... args) const
            -> decltype(func(std::forward<TArgs>(args)...))
    {
        log_context::set_current(cxt);
        return func(std::forward<TArgs>(args)...);
    }
};

template <typename F, typename... TArgs>
auto bind_with_context(F f, TArgs&&... args)
        -> context_binder<decltype(std::bind(f, std::forward<TArgs>(args)...))>
{
    return std::bind(f, std::forward<TArgs>(args)...);
}

如果有人试图将TArgs分配给context_binder(非整数的构造函数尝试抓取std::function),则会扩展为operator()