If you need to iterate over a map, the easiest way is to use tuples, since getting the right type for typedef is unpleasant. Here's how you can use tuples:
std::map<int, double> my_map; int key; double value; BOOST_FOREACH(boost::tie(key, value), my_map) { ... }
Just a note, commas will work here because the brackets are located around the key and value. The preprocessor only understands commas and brackets (and c99 requires it to also understand quotes). Thus, it cannot parse <> into std::pair<int, double> . However, we can use parentheses to help the preprocessor. For example, if we have a function that returns a container that is called like this:
BOOST_FOREACH(int i, foo<int, int>()) { ... }
So, we can put brackets around the expression, and this will help the preprocessor:
BOOST_FOREACH(int i, (foo<int, int>())) { ... }
However, in the case of the map, we cannot put brackets around the declaration (because it is not an expression). So this will not work:
BOOST_FOREACH((std::pair<int, double> p), my_map) { ... }
Because it is converted to something like (std::pair<int, double> p) = *it , and this, of course, is not true in C ++. But using a tie will work:
BOOST_FOREACH(tie(key, value), my_map) { ... }
We just need to declare the key and value outside the loop (as shown above). In addition, it can make the cycle more meaningful. You can write key instead of p.first and value instead of p.second .