c++boostboost-foreach

using boost foreach with items that are themselves templates


I have a std::deque< std::pair<int, int> > that I would like to iterate over using BOOST_FOREACH.

I tried the following:

  #define foreach_ BOOST_FOREACH

  // declaration of the std::deque
  std::deque< std::pair<int, int> > chosen;

  foreach_( std::pair<int,int> p, chosen )
  {
     ... 
  }

But when I compile this (in Visual Studio ) I get the following errors:

warning C4002: too many actual parameters for macro 'BOOST_FOREACH'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ')' before '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : ')'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ';' before '{'
1>c:\users\beeband\tests.cpp(133): error C2181: illegal else without matching if

What is the correct way to use BOOST_FOREACH with this deque?


Solution

  • As the author of BOOST_FOREACH, I ask you to please stop using it. It was a hack whose time has come and gone. Please, please, please use C++11's range-base for loops and let BOOST_FOREACH die.