[lambda] Can't see the wood for the trees"

I have a little code snippet... #include <iostream> #include "boost/range.hpp" #include "boost/function.hpp" #include "boost/lambda/lambda.hpp" int main( ) { using namespace boost::lambda; typedef int block[2][4]; boost::function< int(boost::range_value< block >::type) > third = _1[2]; block values = {{0,1,2,3},{4,5,6,7}}; std::for_each(boost::begin(values), boost::end(values), std::cout << third << "\n" ); } which I want to print the third element of each row of values. I can see that what I've written is not quite right, since the compiler quite reasonably complains that it doesn't kknow how to print a function object, but I can't seem to write the correct incantation! The error message, if it helps is... $ g++ -o junk.exe junk.cpp junk.cpp: In copy constructor `std::basic_ios<char, std::char_traits<char> th _InputIterator = int (*)[4], _Function = std::basic_ostream<char, std::char_traits<char> >]' /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function `_Function std::for_each(_InputIterator, _Inp utIterator, _Function) [with _InputIterator = int (*)[4], _Function = std::basic_ostream<char, std::char_traits<char> >] ': junk.cpp:17: instantiated from here /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:158: error: no match for call to `(std::basic_ostream<char , std::char_traits<char> >) (int[4])' Thank Folks! - Rob.

Comments are inline... On Fri, Nov 21, 2008 at 1:01 PM, Robert Jones <robertgbjones@gmail.com>wrote:
#include <boost/lambda/lambda.hpp>
If you replace the above line with ... std::for_each(boost::begin(values), boost::end(values), std::cout << boost::lambda::bind(third, _1) << "\n"); ... it all appears to work.
}
Although, it clearly isn't optimal in terms of performance. Perhaps accessing the third element could be better achieved with a functor templatised on the index value?
I hope this all helps. Regards, Neil Groves
participants (2)
-
Neil Groves
-
Robert Jones