Lambda library types as indices in for_each

B

Brandt Dusthimer

Hi all. I was wondering, is there any way of using the boost lambda types
as an index for a container? For example:

for_each(indexList.begin(), indexList.end(),
(
somelist[_1].someFunction()
));

Right now it doesn't work because:

error: no match for 'operator[]' in 'somelist[boost::lambda::unnamed::_1]'

Anyway of getting that _1 to turn into, say, an int or string or whatever
type the elements in indexList are? I'm guessing casting's not the
answer :).

Thanks!

Brandt Dusthimer
 
D

David Harmon

On 11 Nov 2006 22:37:39 GMT in comp.lang.c++, "Brandt Dusthimer"
for_each(indexList.begin(), indexList.end(),
(
somelist[_1].someFunction()
));

Right now it doesn't work because:

error: no match for 'operator[]' in 'somelist[boost::lambda::unnamed::_1]'

Quoted from the boost lambda documentation:

Assignment and subscript operators

These operators must be implemented as class members. Consequently, the
left operand must be a lambda expression. For example:

int i;
_1 = i; // ok
i = _1; // not ok. i is not a lambda expression

There is a simple solution around this limitation, described in the
section called “Delaying constants and variables”. In short, the left
hand argument can be explicitly turned into a lambda functor by wrapping
it with a special var function:

var(i) = _1; // ok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top