【问题标题】:How to create a function with argument that would be result of boost::bind?如何使用 boost::bind 的结果创建具有参数的函数?
【发布时间】:2011-08-31 05:37:46
【问题描述】:

所以我想创建一个类似的函数:

void proxy_do_stuff(boost::bind return_here)
{
  return_here(); // call stuff pased into boost::bind
}

我可以这样称呼它:

proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument));

这样的事情怎么办?

【问题讨论】:

    标签: c++ boost boost-bind


    【解决方案1】:

    boost::bind 的返回类型是 boost::function 类型。见下文:

    void proxy_do_stuff(boost::function<void()> return_here)
    {
        return_here(); // call stuff pased into boost::bind
    }
    

    【讨论】:

    • +1 返回类型其实是 boost::_bi::bind_t, boost::_bi::list3, boost::_bi::value, boost::_bi::value > > 但您将其转换为 boost::function
    【解决方案2】:
    #include <boost/bind.hpp>
    
    template<typename T>
    void proxy_do_stuff(T return_here)
    {
        return_here(); // call stuff pased into boost::bind
    }
    
    struct myclass
    {
        void myfunction(int, int)
        {
        }
        void foo()
        {
            int my_function_argument_value = 3;
            int etc_fun_argument= 5;
            proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument));
        }
    };
    
    int main()
    {
        myclass c;
        c.foo();
        return 0;
    }
    

    【讨论】:

    • 以及如何使用该功能?我们不应该像proxy_do_stuff&lt;bla-bla-bla&gt;(...) 这样称呼它吗?
    • 我添加了如何使用它部分。不需要bla-bla-bla。
    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多