# How to store a function copy/pointer w/VARIABLE arguments?

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Heres what I want to do...
//I start with a function...
void foo( int val ) { //do something with val }

//now I want to store/wrap the function somehow, so it can be called later
boost::function( void() ) func;

//however, unless I HARD CODE the 'val' argument, things wont compile...

//works fine
func = boost::bind( &foo, 25 );
func(); //is the same as foo(25);

//does NOT work :(
func = boost::bind( &foo, _1 );
func(44); //SHOULD be the same as foo(44), but doesnt work
func(78); //SHOULD be the same as foo(78), but doesnt work

Can anyone tell me why this is? Or more importantly, how I can make it 'work'? Thanks!

##### Share on other sites
You said that func does not have arguments:
boost::function( void() ) func;

Then, you tried to call it with an integer argument:
func(44);

So, you must have been wrong on one of these accounts.

##### Share on other sites
Oh? So my problem was with how I declared func? I dont have much experience with boost::function, how SHOULD I have declared it?

like... boost::function<void()>(int) func; ?

##### Share on other sites
The documentation advises either function<void (int)> or function<void, int>.

##### Share on other sites
Hmm well were one step closer, however I still get an error when trying to bind...

	boost::function< void(), int > func;	func = boost::bind( &foo, _1 );

doesnt work :(

##### Share on other sites
Any reason why you use boost::function<void(), int> as the function type?

##### Share on other sites
No reason. If there is another way to do what im trying to do, im all for it. I just always try and use boost because I use it for lots of other things (threads, arrays, ect..)

##### Share on other sites
If you have no reason to use it, why not use one of the two types I suggested in my previous response?

##### Share on other sites
Because that would be too easy!

Hah ok I see now, I just assumed you HAD to use void() in order to speicivy a void return. However now that I look at it, - boost::function<void(int)> is such a more logical syntax.

Thanks! Works great!

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013208
×