boost lambda problems
Hi, the following code crashs: #include <boost/cstdint.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/algorithm.hpp> #include <cstring> // memset #include <iostream> union block { char buffer[512]; int integer[128]; }; std::size_t sum( block* blk ) { using namespace boost::lambda; std::size_t sum = 0; std::for_each( &blk->buffer, &blk->buffer + sizeof( blk->buffer ), sum += *_1 ); return sum; } int main() { using namespace boost::lambda; block blk; std::memset( &blk.buffer, '1', sizeof( blk.buffer ) ); std::for_each( &blk.buffer, &blk.buffer + sizeof( blk.buffer ), std::cout << *_1 << '\n' ); std::cout << sum( &blk ) << std::endl; } I'm not sure for the iterator initialization - maybe this fails. I did not wrote C style iterators for a long time and forgot this obviously. What's up here? The goal is the get the sum of the chat buffer. Do I have to protect the sum variable in function sum? Thanks, Olaf
Steven Watanabe schrieb:
AMDG
Olaf Peter wrote:
Thanks Steven ,
std::for_each( &blk->buffer, &blk->buffer + sizeof( blk->buffer ), sum += *_1 );
with this I get huge compiler errors (very long).
You don't need to dereference the iterator. Lambda dereferences it for you.
That's it, I was looking on the wrong place ... Thanks, Olaf
participants (2)
-
Olaf Peter
-
Steven Watanabe