Hi, what kind of object does boost::bind return when binding to a class member variable ? (as opposed to a member method). Is it a function object too? More in particular, I would like to write a generic free function that applies an operation on a particular member variable, passed in with boost::bind. Something like this: struct A { int value; }; void f(bind_return_type& membervar) { membervar = 3; } A a; f(bind(&A::value, a)); Thanks. --nico -- nico galoppo von borries | address: 105 creel st., chapel hill comp. graphics phd. student | north carolina, 27516 USA UNC, chapel hill | phone: +1 (919) 962-1898 (office) | +1 (919) 942-7609 (home) | email: nico at cs dot unc dot edu | homepage: http://www.ngaloppo.org
On 3/29/06, Nico Galoppo
Hi,
what kind of object does boost::bind return when binding to a class member variable ? (as opposed to a member method). Is it a function object too?
More in particular, I would like to write a generic free function that applies an operation on a particular member variable, passed in with boost::bind. Something like this:
struct A { int value; };
void f(bind_return_type& membervar) { membervar = 3; }
A a; f(bind(&A::value, a));
Thanks.
--nico
Couldn't you just do void f(int& membervar) { membervar = 3; } A a; f(a.value); ? To get back to bind return types - they're not simple. Your best bet is to use boost::function to hold the result of boost::bind if you want to keep it around. Just make sure any variables involved are still alive when the function is eventually invoked. Stuart Dootson
Hi Stuart, thanks for your answer. Stuart Dootson wrote:
void f(int& membervar) { membervar = 3; }
A a; f(a.value);
Well, I probably over-simplified the problem. What I would really like to do is manipulate data members of all instances in a container, where the data member is given as an input parameter. struct A { int value; int othervalue; } container<A> array; void f(container<A> & array, membervarref) { BOOST_FOREACH(container<A>::value_type& element, array) bind(membervarref, element) = 3; } f(array, &A::value); f(array, &A::othervalue); --nico -- nico galoppo von borries @ address: 105 creel st., chapel hill comp. graphics phd. student @ north carolina, 27516 USA UNC, chapel hill @ phone: +1 (919) 962-1898 (office) @ +1 (919) 942-7609 (home) @ email: nico at cs dot unc dot edu @ homepage: http://www.cs.unc.edu/~nico --- debian linux :: vim powered
On 3/30/06, Nico Galoppo
Well, I probably over-simplified the problem. What I would really like to do is manipulate data members of all instances in a container, where the data member is given as an input parameter.
struct A { int value; int othervalue; }
container<A> array;
void f(container<A> & array, membervarref) { BOOST_FOREACH(container<A>::value_type& element, array) bind(membervarref, element) = 3; }
f(array, &A::value); f(array, &A::othervalue);
--nico
Well, how about this (using you definition of A):
typedef std::vector<A> As;
typedef int A::*Setter;
void Set(As& as, Setter setter, int value)
{
using namespace boost::lambda;
std::for_each(as.begin(), as.end(), bind(setter, _1) = value);
}
void func()
{
As as;
Set(as, &A::a, 3);
Set(as, &A::b, 5);
}
If you want to decouple from A a bit more, you could change the
definition of Setter like below
typedef boost::function
Hi Stuart, thanks for the solution. It seems like what I am looking for, I'll try it out asap. However, I am confused by the typedef below. typedef int A::*Setter; Could you explain what is going on here? --nico Stuart Dootson wrote:
On 3/30/06, Nico Galoppo
wrote: <snip> Well, I probably over-simplified the problem. What I would really like to do is manipulate data members of all instances in a container, where the data member is given as an input parameter.
struct A { int value; int othervalue; }
container<A> array;
void f(container<A> & array, membervarref) { BOOST_FOREACH(container<A>::value_type& element, array) bind(membervarref, element) = 3; }
f(array, &A::value); f(array, &A::othervalue);
--nico
Well, how about this (using you definition of A):
typedef std::vector<A> As; typedef int A::*Setter;
void Set(As& as, Setter setter, int value) { using namespace boost::lambda; std::for_each(as.begin(), as.end(), bind(setter, _1) = value); }
void func() { As as;
Set(as, &A::a, 3); Set(as, &A::b, 5); }
If you want to decouple from A a bit more, you could change the definition of Setter like below
typedef boost::function
Setter; and it'll compile and run OK (tested with VC7.1).
Stuart Dootson _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- nico galoppo von borries @ address: 105 creel st., chapel hill comp. graphics phd. student @ north carolina, 27516 USA UNC, chapel hill @ phone: +1 (919) 962-1898 (office) @ +1 (919) 942-7609 (home) @ email: nico at cs dot unc dot edu @ homepage: http://www.cs.unc.edu/~nico --- debian linux :: vim powered
Nico Galoppo wrote:
Hi Stuart,
thanks for the solution. It seems like what I am looking for, I'll try it out asap. However, I am confused by the typedef below.
typedef int A::*Setter;
Could you explain what is going on here?
It defines Setter to be an alias for the type "pointer to 'integer member of A'". Sebastian Redl
Thanks Mathias! Do you have an example of how that would work? I have no experience with boost::any. --nico loufoque wrote:
Stuart Dootson wrote :
Well, how about this (using you definition of A):
typedef std::vector<A> As; typedef int A::*Setter;
[...]
Such a solution only works if all member variables are ints. If it needs to work with any type, boost::any could be used.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- nico galoppo von borries | address: 105 creel st., chapel hill comp. graphics phd. student | north carolina, 27516 USA UNC, chapel hill | phone: +1 (919) 962-1898 (office) | +1 (919) 942-7609 (home) | email: nico at cs dot unc dot edu | homepage: http://www.ngaloppo.org
Nico Galoppo wrote:
Do you have an example of how that would work? I have no experience with boost::any.
It was just an idea actually, and not a very good one : I couldn't even
make it work.
I'm just discovering boost::any myself and I thought you could bind on
it automagically.
Using templates is simpler.
template
Nico Galoppo wrote:
Hi,
what kind of object does boost::bind return when binding to a class member variable ? (as opposed to a member method). Is it a function object too?
Yes, the result is an unary function object, the parameter being the object to retrieve the variable of, and which returns a reference to the member variable. The exact type of course is a complicated bind expression, so a non-template is not particularly suited for taking it. Sebastian Redl
participants (4)
-
loufoque
-
Nico Galoppo
-
Sebastian Redl
-
Stuart Dootson