1

How can I template these functions?

boost::function< void(int) > binder( void(*func)(int, char), int a1, char a2 )
{
    return boost::bind( func, a1, a2 );
}

void proxy( boost::function< void(int) > func, int a1 )
{
    boost::bind( func, a1 )();
}

I tried the following with no success:

template< typename R, typename A1, typename A2 >
static boost::function< void(int) > binder( R(*func)(A1,A2), A1 a1, A2 a2 )
{
    return boost::bind( func, a1, a2 );
}

template< typename A1 >
static void proxy( boost::function< void(A1) > func, A1 a1 )
{
    boost::bind( func, a1 )();
}

It would be nice if I could do without binder(). This is how I intend to use them:

void print( int i, char c );
boost::signals2::signal.connect(
    boost::bind(
        &proxy,
        boost::bind(
            &binder,
            &print,
            _1,
            'a'
            ),
        _1
        )
    );

I checked out the following with no luck:

how-to-use-manipulate-return-value-from-nested-boostbind

perform-argument-substitution-on-nested-boostbind-without-composition

can-i-use-boost-bind-with-a-function-template

Community
  • 1
  • 1

1 Answers1

2

You need to spell function pointers right:

R(*func)(A1, A2)

You will also need to specify the template parameters for forming a function pointer: Remember that binder is not a function, but a template!

&binder<void, int, char>
&proxy<int>

Finally, you're not getting your signal variable right. Declare it like this:

boost::signals2::signal<void(int)> sig;

Then use:

sig.connect( /* all that stuff */ );
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I applied the change in the posted code. Still did not compile. – user1005752 Nov 12 '11 at 01:14
  • Thank you. I still get a little confused by when template arguments are deduced. I will keep in mind what you mentioned above though. Sorry about the signals2 part. I am aware of that part, but was trying to "minimize" the code I posted. – user1005752 Nov 12 '11 at 01:52
  • @user1005752: Well, think about it a bit... when you're forming the function pointer, there's nothing to deduce from, so you have to specify the template parameters. How else could the compiler know them? – Kerrek SB Nov 12 '11 at 01:52
  • I was assuming since the function pointer is passed into bind, that the template types could be deduced by the remaining parameters passed to the bind. I guess I was mistaken. Thanks for helping clear things up! – user1005752 Nov 14 '11 at 06:32