0

有人可以解释一下如何使用 BOOST_FOREACH 枚举 BOOST_ENUM 吗?下面的例子表明我让它与 std::for_each 一起工作,但不能与 BOOST_FOREACH 一起工作。

示例代码:

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);

}

提前谢谢了!

编辑:如答案中所述,该代码确实适用于最新的 boost 代码库。

4

1 回答 1

1

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?

于 2010-10-31T15:51:27.023 回答