下面的代码段在
如果我用一个for循环(使用从sectors.begin()到sectors.end()的迭代器)替换BOOST_FOREACH循环,则它可以正常工作(没有无限循环)。
版本和其他信息:
- Boost: 1.40.0 - gcc: 4.1.2 - 架构:x86_64 - 只有在发布版本中才会出现此行为;如果我进行调试构建,则它按预期工作。 - 在Visual Studio下编译时,它按预期工作,即没有无限循环。
响应mkb的问题,这是当我运行gcc -E时得到的结果:
BOOST_FOREACH
语句内部进入了一个无限循环,我无法弄清楚原因。根据 Boost 文档,好像在 BOOST_FOREACH
循环内部使用 "break" 是可以的。您知道可能出了什么问题吗?std::vector<std::wstring> sectors = getSectors();
if (!_sectorCodes.empty()) { // _sectorCodes is a std::set<std::wstring>.
bool ok = false; // did we find the sector code we wanted?
BOOST_FOREACH(Symbol sector, sectors) {
if (_sectorCodes.find(sector) != _sectorCodes.end()) {
ok = true;
break;
}
}
if (!ok) return NULL;
}
如果我用一个for循环(使用从sectors.begin()到sectors.end()的迭代器)替换BOOST_FOREACH循环,则它可以正常工作(没有无限循环)。
版本和其他信息:
- Boost: 1.40.0 - gcc: 4.1.2 - 架构:x86_64 - 只有在发布版本中才会出现此行为;如果我进行调试构建,则它按预期工作。 - 在Visual Studio下编译时,它按预期工作,即没有无限循环。
响应mkb的问题,这是当我运行gcc -E时得到的结果:
if (!_sectorCodes.empty()) {
bool ok = false;
if (boost::foreach_detail_::auto_any_t _foreach_col148 = boost::foreach_detail_::contain( (sectors) , (true ? 0 : boost::foreach_detail_::or_( boost::foreach_detail_::and_( boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(sectors)) , (true ? 0 : boost::foreach_detail_::is_rvalue_( (true ? boost::foreach_detail_::make_probe(sectors) : (sectors)), 0))) , boost::foreach_detail_::and_( boost::foreach_detail_::not_(boost_foreach_is_noncopyable( boost::foreach_detail_::to_ptr(sectors) , boost_foreach_argument_dependent_lookup_hack_value)) , boost_foreach_is_lightweight_proxy( boost::foreach_detail_::to_ptr(sectors) , boost_foreach_argument_dependent_lookup_hack_value)))))) {} else if (boost::foreach_detail_::auto_any_t _foreach_cur148 = boost::foreach_detail_::begin( _foreach_col148 , (true ? 0 : boost::foreach_detail_::encode_type(sectors, boost::foreach_detail_::is_const_(sectors))) , (true ? 0 : boost::foreach_detail_::or_( boost::foreach_detail_::and_( boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(sectors)) , (true ? 0 : boost::foreach_detail_::is_rvalue_( (true ? boost::foreach_detail_::make_probe(sectors) : (sectors)), 0))) , boost::foreach_detail_::and_( boost::foreach_detail_::not_(boost_foreach_is_noncopyable( boost::foreach_detail_::to_ptr(sectors) , boost_foreach_argument_dependent_lookup_hack_value)) , boost_foreach_is_lightweight_proxy( boost::foreach_detail_::to_ptr(sectors) , boost_foreach_argument_dependent_lookup_hack_value)))))) {} else if (boost::foreach_detail_::auto_any_t _foreach_end148 = boost::foreach_detail_::end( _foreach_col148 , (true ? 0 : boost::foreach_detail_::encode_type(sectors, boost::foreach_detail_::is_const_(sectors))) , (true ? 0 : boost::foreach_detail_::or_( boost::foreach_detail_::and_( boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(sectors)) , (true ? 0 : boost::foreach_detail_::is_rvalue_( (true ? boost::foreach_detail_::make_probe(sectors) : (sectors)), 0))) , boost::foreach_detail_::and_( boost::foreach_detail_::not_(boost_foreach_is_noncopyable( boost::foreach_detail_::to_ptr(sectors) , boost_foreach_argument_dependent_lookup_hack_value)) , boost_foreach_is_lightweight_proxy( boost::foreach_detail_::to_ptr(sectors) , boost_foreach_argument_dependent_lookup_hack_value)))))) {} else for (bool _foreach_continue148 = true; _foreach_continue148 && !boost::foreach_detail_::done( _foreach_cur148 , _foreach_end148 , (true ? 0 : boost::foreach_detail_::encode_type(sectors, boost::foreach_detail_::is_const_(sectors)))); _foreach_continue148 ? boost::foreach_detail_::next( _foreach_cur148 , (true ? 0 : boost::foreach_detail_::encode_type(sectors, boost::foreach_detail_::is_const_(sectors)))) : (void)0) if (boost::foreach_detail_::set_false(_foreach_continue148)) {} else for (Symbol sector = boost::foreach_detail_::deref( _foreach_cur148 , (true ? 0 : boost::foreach_detail_::encode_type(sectors, boost::foreach_detail_::is_const_(sectors)))); !_foreach_continue148; _foreach_continue148 = true) {
if (_sectorCodes.find(sector) != _sectorCodes.end()) {
ok = true;
break;
}
}
if (!ok) return PatternFeatureSet_ptr();
}
这个扩展的一个显著特点是有两个嵌套的for循环。我无法弄清楚内部循环和外部循环发生了什么,但是否可能(正如David所建议的那样)我只是从内部循环中退出,并且BOOST_FOREACH由于某种原因没有处理得很好?
BOOST_FOREACH
的规则,但如果你使用任何带有“for each”名称的东西,然后使用break来离开循环,那么你就是在欺骗读者。 - James Kanze