【问题标题】:How can I use a boost function to transform the parameter types?如何使用 boost 函数来转换参数类型?
【发布时间】:2014-09-24 00:28:31
【问题描述】:

我想创建一个具有以下签名的 boost 函数对象:

void (int, boost::uuid);

但是,我想将它绑定到以下形式的函数:

void (SomeType, boost::uuid)

SomeType 参数来自另一个函数调用,所以如果我直接调用它,它看起来像:

SomeType myOtherFunction(int);//Prototype 
... 
myFunction(myOtherFunction(int), myUUID);

换句话说,我希望顶级函数对象完全隐藏 SomeType 的概念用户对 myOtherFunction 的调用。有没有办法通过 boost::bind 调用创建的一个或多个 boost::function 对象来做到这一点?

【问题讨论】:

    标签: c++ boost boost-function


    【解决方案1】:

    功能组成:Live On Coliru

    #include <boost/uuid/uuid.hpp>
    
    struct SomeType {};
    SomeType myOtherFunction(int) { return SomeType(); }
    void foo(SomeType, boost::uuids::uuid) {}
    
    #include <boost/bind.hpp>
    #include <boost/function.hpp>
    
    int main()
    {
        boost::function<void(int, boost::uuids::uuid)> composed;
    
        composed = boost::bind(foo, boost::bind(myOtherFunction, _1), _2);
    }
    

    无论如何,在 c++11 中你当然会写 [](int i, uuid u) { return foo(myOtherFunction(i), u); }

    【讨论】:

      猜你喜欢
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多