loop vs. for_each & boost::bind
Hi, I have some code that runs fine: for (Partition::iterator pi = renderRegions.begin(); pi != renderRegions.end(); ++pi); (*pi)->render(<t, foo, si); I try to replace it by for_each & boost::bind, std::for_each(renderRegions.begin(), renderRegions.end(), boost::bind(&Region::render, _1, <t, foo, si)); but si doesn't get modified, while it should be passed as reference. Some more relevant code: class Region { ... void render(const LightTransportType* ltt, WorkAppraiser& wa, image::SampleImage& result); ... }; typedef std::vector< Region* > Partition; Partition renderRegions; Is this normal behaviour? -- With kind regards, Mathieu
"Mathieu De Zutter"
Hi,
I have some code that runs fine:
for (Partition::iterator pi = renderRegions.begin(); pi != renderRegions.end(); ++pi); (*pi)->render(<t, foo, si);
I try to replace it by for_each & boost::bind,
std::for_each(renderRegions.begin(), renderRegions.end(), boost::bind(&Region::render, _1, <t, foo, si));
but si doesn't get modified, while it should be passed as reference.
Try using: std::for_each(renderRegions.begin(), renderRegions.end(), boost::bind(&Region::render, _1, <t, foo, boost::ref(si)));
-- With kind regards, Mathieu
Pablo Aguilar
Hello :
Try using boost::ref to avoid creating a copy of the arguments (see
http://www.boost.org/doc/html/ref.html ) :
std::for_each( rr.begin(),
rr.end(),
boost::bind( boost::mem_fn( &Region::render ),
_1,
boost::ref( ltt ),
foo,
boost::ref( si )
)
);
Good luck!
On 4/20/05, Mathieu De Zutter
Hi,
I have some code that runs fine:
for (Partition::iterator pi = renderRegions.begin(); pi != renderRegions.end(); ++pi); (*pi)->render(<t, foo, si);
I try to replace it by for_each & boost::bind,
std::for_each(renderRegions.begin(), renderRegions.end(), boost::bind(&Region::render, _1, <t, foo, si));
but si doesn't get modified, while it should be passed as reference.
Some more relevant code:
class Region { ... void render(const LightTransportType* ltt, WorkAppraiser& wa, image::SampleImage& result); ... };
typedef std::vector< Region* > Partition; Partition renderRegions;
Is this normal behaviour?
-- With kind regards, Mathieu _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Mathieu De Zutter wrote:
std::for_each(renderRegions.begin(), renderRegions.end(), boost::bind(&Region::render, _1, <t, foo, si));
but si doesn't get modified, while it should be passed as reference.
You need to wrap foo and si in a ref(). std::for_each(renderRegions.begin(), renderRegions.end(), boost::bind(&Region::render, _1, <t, boost::ref(foo), boost::ref(si))); KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com
participants (4)
-
Kevin Heifner
-
Mathieu De Zutter
-
Pablo Aguilar
-
steve ahlgren