c++boostboost-function

How can I use a boost function to transform the parameter types?


I want to create a boost function object of the following signature:

void (int, boost::uuid);

However, I would like to bind it to a function of the following form:

void (SomeType, boost::uuid)

Where the SomeType argument comes from another function call, so that if I were to call it straight out it would look like:

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

In other words, I want the top level function object to completely hide the concept of SomeType and the call to myOtherFunction from the user. Is there a way to do this with one or more boost::function objects created with boost::bind calls?


Solution

  • Functional composition: 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);
    }
    

    Anyways, in c++11 you'd write [](int i, uuid u) { return foo(myOtherFunction(i), u); } of course