如何使用 BOOST_FOREACH 枚举 BOOST_ENUM?
有人可以解释一下如何使用 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 的最新代码库。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的示例代码在 gcc 4.5.1 和 vc2010 上编译并运行得很好(即添加相应的 #include 之后)。我尝试使用保管库中的 enum_rev4.6 。您看到什么编译错误?
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?