c++boostboost-foreach

How to enumerate a BOOST_ENUM with BOOST_FOREACH?


Can somebody please explain me how to enumerate a BOOST_ENUM using BOOST_FOREACH ? The example below show that I got it to work with std::for_each, but not with BOOST_FOREACH.

Sample code :

BOOST_ENUM_VALUES(  MyEnum, 
  const char *, 
      (xMin)("xMin")
      (xMax)("xMax")
      (yMin)("yMin")
      (yMax)("yMax")
);

void blah(const MyEnum & val)  // working demo with std_foreach
{
  std::cout << "index=" << val.index() << " val=" << val.value() << " str=" << val.str() << std::endl;
}


void foo()
{
  //BOOST_FOREACH : does not compile...
  BOOST_FOREACH(MyEnum myEnum, MyEnum() ) // I tried to construct a "dummy" enum in order to use its iterator with no luck...
  {
    std::cout << "index=" << myEnum.index() << " val=" << myEnum.value() << " str=" << myEnum.str() << std::endl;
  }

  //std::for_each : works...
  std::for_each(MyEnum::begin(), MyEnum::end(), blah);

}

Many thanks in advance!

Edit: as mentioned in the answer, the code does work with the newest codebase of boost.


Solution

  • Your example code above compiles and runs just fine for me with gcc 4.5.1 and vc2010 (after adding the corresponding #include's, that is). I tried with enum_rev4.6 from the vault. What compilation errors do you see?