I have a Visual Studio 2008 C++03 project where I would like to use a boost::function
object to set the value of a pointer. Something like this:
boost::function< void( int* ) > SetValue;
boost::function< int*() > GetValue;
int* my_value_;
SetValue = boost::bind( my_value_, _1 ); // how should this look?
GetValue = boost::bind( my_value_ ); // and this?
int v;
SetValue( &v );
assert( my_value_ == &v );
int* t = GetValue();
assert( t == my_value_ );
Is there a way to do this or do I need an intermediate function like:
void DoSetValue( int* s, int* v ) { s = v; };
SetValue = boost::bind( DoSetValue, my_value_, _1 );
Thanks
For bind
to work that way, you would need a pointer to operator=( int* )
. Of course, there is no such thing, so you need an intermediate function.
If you could use lambda
or phoenix
, there are ways to make a function-object that assigns something to something else. It depends on which library you use, but it would look somewhat like this:
bl::var( my_value_ ) = bl::_1;