
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