Boost logo

Boost :

From: brownell_at_[hidden]
Date: 2001-10-15 15:15:27


I have a question that is probably better asked in code than in
words, but I will try the words route first, and if that doesn't
work, I have pasted code that may explain the situation better.

I have one function that takes 1 arg (call it Func1), another that
takes 2 args (call it Func2). Both these functions take
boost::functionX params. Somehow, I need to bind a value to the
first arg in Func2, and pass that new function to Func1.

I can not wait for the return value of Func1, as these function are
used to update algorithm progress.

Make any sense? Hopefully the code will.

Thanks for any and all help,
Dave Brownell

#include<iostream>

#include "boost\bind.hpp"
#include "boost\Function.hpp"

typedef boost::function1<bool, unsigned long>
OneArgFunction;
typedef boost::function2<bool, unsigned long, unsigned long>
TwoArgFunction;

inline bool WriteOne(unsigned long ul1)
{
    std::cout << ul1 << std::endl;
    return(true);
}

inline bool WriteTwo(unsigned long ul1, unsigned long ul2)
{
    std::cout << "(" << ul2 << ") " << ul1 << std::endl;
    return(true);
}

bool DoIt_One(OneArgFunction &f)
{
    if(f.empty() == false)
        f(1);

    return(true);
}

bool DoIt_Two(TwoArgFunction &f2, unsigned long ulValue)
{
    if(f.empty() == false)
        return(false);

    //BEGIN PROBLEM
    OneArgFunction f_oneArg;

    f_oneArg = boost::bind(f2, ulValue, _1);
    //END PROBLEM
    return(DoIt_One(f_oneArg));
}

int main(void)
{
    TwoArgFunction f2 = &WriteTwo;

    DoIt_Two(f2, 2);

    return(true);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk