Boost logo

Boost :

From: Dirk Gerrits (dirkg_at_[hidden])
Date: 2002-05-11 05:08:34


Hi,

I am having a problem when combining boost::function with function
overloading. I'm trying to overload a function so that it can take
a funtion0<void> or a function1<void, int> and then call another
function with that function object.

However this 'other' function can only take function objects which
can be called without parameters. For this to work, the
function1<void, int> version should bind a constant integer to its
parameter and then pass the bound function object to the other
function.

Here's a piece of code to clarify:

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

template< class Function >
void DoTheWork(const Function& func)
{
    // more code here
    func();
    // more code here
}

void func1() { std::cout << "func1()"; };
void func2(int a) { std::cout << "func2(" << a << ")"; };
void func3(const int& a) { std::cout << "func3(" << a << ")"; };

void Test(boost::function1<void, int> test)
{
    std::cout << "function1()\n";
    DoTheWork( boost::bind<void>(test, 5) );
}
/*
void Test(boost::function0<void> test)
{
    std::cout << "function0(";
    DoTheWork( test );
    std::cout<< ")\n";
}
*/
int main(int argc, char* argv[])
{
    //Test(&func1);
    Test(&func2);
    Test(&func3);
}

If I uncomment those lines, then the compiler complaints that the overloads
are ambiguous. And this is understandable. So I was wondering if there is a
workaround for this. My initial thought was something like this:

Test( make_function(&func1) );
Test( make_function(&func2) );
Test( make_function(&func3) );

make_function should return the apropriate boost::functionN object based on
template argument deduction. However I've not been able to find a function
with this functionality. I could write my own of course, but that would be
extremely tedious for increasing numbers of parameters. (And I'm not such a
hero with code generation scripts.)

So I was wondering if Boost contains a function like this? Or if you know
of different methods to accomplish this task. (I haven't been able to come
up with any so far. :(

Any help is appreciated.

Regards,

Dirk Gerrits


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