Boost logo

Boost Users :

From: Rob Caldecott (robert.caldecott_at_[hidden])
Date: 2006-09-18 12:52:31


OK, I know BOOST_FOREACH isn't part of the official (1.33.1) build yet, but
hopefully some people here are using this excellent algorithm and can help
me out... (note that I am using MS Visual Studio 2005):

How can I use BOOST_FOREACH to iterate through a map? For example, the
following won't build:

std::map<int, int> m;
BOOST_FOREACH(std::pair<int, int> p, m)
{
}

I get the following error:

    warning C4002: too many actual parameters for macro 'BOOST_FOREACH'

However, the following will work OK:

std::map<int, int> m;
std::pair<int, int> p;
BOOST_FOREACH(p, m)
{
}

But is there a way to declare the pair within the FOREACH loop? Note that I
also tried the following, with no success:

std::map<int, int> m;
BOOST_FOREACH(std::map<int, int>::value_type p, m)
{
}

A typedef also seems to work, e.g.:
std::map<int, int> m;
typedef std::pair<int, int> mpair;
BOOST_FOREACH(mpair p, m)
{
}

Can the pair be declared without a typedef? Is this a limitation of the MS
compiler?

Thanks in advance.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net