Boosters: Try as I might, I cannot figure a way of converting the following code snippet to use std::for_each with boost::lamdba. Any ideas? unsigned int table[1000]; for ( unsigned int* it = table_; it != table_ + (sizeof(table_) / sizeof(unsigned int)); ++i ) { *it = ((int)it % 3); } The pointer to int cast is the killer, the docs mention the ret<> template briefly, but it does not seem to help... Cheers Ian
Ian wrote:
Boosters:
Try as I might, I cannot figure a way of converting the following code snippet to use std::for_each with boost::lamdba. Any ideas?
unsigned int table[1000]; for ( unsigned int* it = table_; it != table_ + (sizeof(table_) / sizeof(unsigned int)); ++i ) { *it = ((int)it % 3); }
The pointer to int cast is the killer, the docs mention the ret<> template briefly, but it does not seem to help...
Use the cast objects: ll_static_cast<int>(_1) not tested, but it might work: namespace l = boost::lambda; // use this if You wanna get happy! std::for_each(begin, end, l::_1 = l::ll_static_cast<int>(_1) % 3); Markus -- Build your own Expression Template Library with Daixtrose! Visit http://daixtrose.sourceforge.net/
Markus Thanks for the reply, but no dice. The idea was to use the address of the current item as a quick and dirty counter modular counter, However this code: std::for_each(begin, end, l::_1 = l::ll_static_cast<int>(_1) % 3); does not take the address but the items value, and unfortunately this: std::for_each(begin, end, l::_1 = l::ll_static_cast<int>(&_1) % 3); does not compile. Now I could get a bit more realistic and just code the thing a bit cleaner like this: int x = 0; for_each( lookup_, lookup_ + (sizeof(lookup_) / sizeof(unsigned int)), _1 = ++var(x) % 3 ); But that's kinda skirting around the real issue which is, if I can't use lambda to *directly* replace code that is trivial with a hand written loop, what chance when the complexity is raised a bit? Cheers Ian
Ian wrote:
Markus
Thanks for the reply, but no dice. The idea was to use the address of the current item as a quick and dirty counter modular counter, However this code:
std::for_each(begin, end, l::_1 = l::ll_static_cast<int>(_1) % 3);
does not take the address but the items value, and unfortunately this:
std::for_each(begin, end, l::_1 = l::ll_static_cast<int>(&_1) % 3);
does not compile. Now I could get a bit more realistic and just code the thing a bit cleaner like this:
int x = 0; for_each( lookup_, lookup_ + (sizeof(lookup_) / sizeof(unsigned int)), _1 = ++var(x) % 3 );
But that's kinda skirting around the real issue which is, if I can't use lambda to *directly* replace code that is trivial with a hand written loop, what chance when the complexity is raised a bit?
It is a feature that your original (nonportable) code cannot be replaced by
a lambda expression. Why not just use the correct version? Or even better:
template
participants (3)
-
Ian
-
Markus Werle
-
Peter Dimov