Understanding Boost.Mpl Fold Algorithm
Hi, I'm having a hard time trying to understand what the placeholders _1 and _2 mean in the context of the mpl::fold algorithm.
From the Boost.Mpl Reference manual:
typedef vector
::type number_of_floats;
BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 ); Could someone give me some help on this ? Best regards, Eduardo
Eduardo Bezerra wrote:
Hi,
I'm having a hard time trying to understand what the placeholders _1 and _2 mean in the context of the mpl::fold algorithm.
From the Boost.Mpl Reference manual:
typedef vector
types; typedef fold< types , int_<0> , if_< is_float<_2>,next<_1>,_1 >
::type number_of_floats;
BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 );
_1 is the result holder, _2 is the current element. Let's for a moment translate the thing to some pseudo-code: number_of_floats = fold(types, 0, lambda(value, element){return is_float(element) ? value+1, value; }); Where fold is function fold(sequence, initial_value, func) { value = initial_value; for each element in sequence { value = func(value, element); } } _1 is value, _2 is element. Sebastian Redl
Hi Eduardo, the fold algorithm iterates over a mpl-sequence. the function needs three arguments as input: the sequence to iterate over, a "starting condition" and a binary operation. at every step of the iteration the algorithm applies two arguments to the binary operation: the first argument is the output of the previous invocation of the operation (at the first step that is the starting condition). the second argument is the currently processed element from your input sequence. the "starting condition" also determines the return type of your fold-operation. about the placeholders you only have to know that "_1" means the first and "_2" stands for the second argument. if you don't have the line "using namespace boost::mpl" in your code, the placeholders are boost::mpl::_1 and boost::mpl::_2. that should be all! the algorithm from the example returns an integral constant with the number of elements in the input sequence that are float types. if is_float applied to the current element returns true, than the integral constant will be incremented by one ("next<_1>"), otherwise it will not be changed and so on. Regards, Karl
Eduardo Bezerra
Hi,
I'm having a hard time trying to understand what the placeholders _1 and _2 mean in the context of the mpl::fold algorithm.
From the Boost.Mpl Reference manual:
typedef vector
types; typedef fold< types , int_<0> , if_< is_float<_2>,next<_1>,_1 >
::type number_of_floats;
BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 );
Could someone give me some help on this ?
<shameless plug> http://www.boost-consulting.com/mplbook contains a good explanation. -- Dave Abrahams Boost Consulting www.boost-consulting.com
Hi Dave,
Re-reading this great book for the 2nd time, and studying chapter
11 I found myself trying to understand the mpl::fold algorithm.
Thanks for this wonderful work.
My best regards,
Eduardo
On 1/8/06, David Abrahams
Eduardo Bezerra
writes: Hi,
I'm having a hard time trying to understand what the placeholders _1 and _2 mean in the context of the mpl::fold algorithm.
From the Boost.Mpl Reference manual:
typedef vector
types; typedef fold< types , int_<0> , if_< is_float<_2>,next<_1>,_1 >
::type number_of_floats;
BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 );
Could someone give me some help on this ?
<shameless plug> http://www.boost-consulting.com/mplbook contains a good explanation.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Eduardo Bezerra
Hi Dave,
Re-reading this great book for the 2nd time, and studying chapter 11 I found myself trying to understand the mpl::fold algorithm.
Sorry, I'm confused. did it help or did it leave you asking the boost-users list for an explanation?
Thanks for this wonderful work.
You're welcome. -- Dave Abrahams Boost Consulting www.boost-consulting.com
Actually after re-reading chapter 11 I got confused about the behavior
of the mpl::fold algorithm.
But the participants of this list kindly help me out. I don't have any more
doubts now.
Best Regards,
Eduardo
On 1/9/06, David Abrahams
Eduardo Bezerra
writes: Hi Dave,
Re-reading this great book for the 2nd time, and studying chapter 11 I found myself trying to understand the mpl::fold algorithm.
Sorry, I'm confused. did it help or did it leave you asking the boost-users list for an explanation?
Thanks for this wonderful work.
You're welcome.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
David Abrahams
-
Eduardo Bezerra
-
Karl Friedrich Walkow
-
Sebastian Redl