The BOOST_PP_LIST_FOLD_LEFT macro folds (or accumulates) the elements of a list left-to-right.


BOOST_PP_LIST_FOLD_LEFT(op, state, list)


A ternary operation of the form op(d, state, elem).  This macro is called for each element in list--each time returning a new state.  This operation is expanded by BOOST_PP_LIST_FOLD_LEFT with the next available BOOST_PP_WHILE iteration, the current state, and the current element.
The initial state of the fold.
The list to be folded.


For the list, (0, (1, (2, BOOST_PP_NIL))), this macro expands to:
op(d, op(d, op(d, state, 0), 1), 2)
Previously, this macro could not be used inside BOOST_PP_WHILE.  There is no longer any such restriction.  It is more efficient, however, to use BOOST_PP_LIST_FOLD_LEFT_d in such a situation.

See Also


Header:  <boost/preprocessor/list/fold_left.hpp>

Sample Code

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/list/fold_left.hpp>

#define LIST (a, (b, (c, BOOST_PP_NIL)))

#define OP(d, state, x) BOOST_PP_CAT(state, x)

BOOST_PP_LIST_FOLD_LEFT(OP, _, LIST) // expands to _abc

Copyright Housemarque Oy 2002
Copyright Paul Mensonides 2002

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at