我知道升压或编译器应该是最后的罪魁祸首,但我在这里看不到其他解释。
我正在使用 msvc 2008 SP1 和 boost 1.43。
在下面的代码片段中执行永远不会离开thirdBOOST_FOREACH 循环
typedef Graph<unsigned, unsigned>::VertexIterator Iter;
Graph<unsigned, unsigned> g;
g.createVertex(0x66);
// works fine
Iter it = g.getVertices().first, end = g.getVertices().second;
for(; it != end; ++it)
;
// fine
std::pair<Iter, Iter> p = g.getVertices();
BOOST_FOREACH(unsigned handle, p)
;
// fine
unsigned vertex_count = 0;
BOOST_FOREACH(unsigned handle, g.getVertices())
vertex_count++;
// oops, infinite loop
vertex_count = 0;
BOOST_FOREACH(unsigned handle, g.getVertices())
vertex_count++;
vertex_count = 0;
BOOST_FOREACH(unsigned handle, g.getVertices())
vertex_count++;
// ... last block repeated 6 times
迭代器代码:
class Iterator
: public boost::iterator_facade<Iterator, unsigned const,
boost::bidirectional_traversal_tag>
{
public:
Iterator()
: list(NULL), handle(INVALID_ELEMENT_HANDLE)
{}
explicit Iterator(const VectorElementsList &list, unsigned handle = INVALID_ELEMENT_HANDLE)
: list(&list), handle(handle)
{}
friend std::ostream&
operator<<(std::ostream &s, const Iterator &it)
{
s << "[list: " << it.list <<", handle: " << it.handle << "]";
return s;
}
private:
friend class boost::iterator_core_access;
void increment()
{
handle = list->getNext(handle);
}
void decrement()
{
handle = list->getPrev(handle);
}
unsigned const& dereference() const
{
return handle;
}
bool equal(Iterator const& other) const
{
return handle == other.handle && list == other.list;
}
const VectorElementsList<T> *list;
unsigned handle;
};
ASM 的一些乐趣:
vertex_count = 0;
BOOST_FOREACH(unsigned handle, g.getVertices())
// initialization
013E1369 mov edi,dword ptr [___defaultmatherr+8 (13E5034h)] // end iterator handle: 0xFFFFFFFF
013E136F mov ebp,dword ptr [esp+0ACh] // begin iterator handle: 0x0
013E1376 lea esi,[esp+0A8h] // begin iterator list pointer
013E137D mov ebx,esi
013E137F nop
// forever loop begin
013E1380 cmp ebp,edi
013E1382 jne main+238h (13E1388h)
013E1384 cmp ebx,esi
013E1386 je main+244h (13E1394h)
013E1388 lea eax,[esp+18h]
013E138C push eax
// here iterator is incremented in ram
013E138D call boost::iterator_facade<detail::VectorElementsList<Graph<unsigned int,unsigned int>::VertexWrapper>::Iterator,unsigned int const ,boost::bidirectional_traversal_tag,unsigned int const &,int>::operator++ (13E18E0h)
013E1392 jmp main+230h (13E1380h)
vertex_count++;
// forever loop end
很容易看出迭代器句柄缓存在 EBP 中,尽管调用了迭代器operator++() 函数,但它永远不会递增。
我已将 Itarator 实现替换为从 std::iterator 派生的实现,并且问题仍然存在,因此这不是 iterator_facade 错误。
此问题仅存在于 msvc 2008 SP1 x86 和 amd64 发行版上。调试构建在 msvc 2008 上,调试/发布构建在 msvc 2010 和 gcc 4.4 (linux) 上运行良好。
此外,BOOST_FOREACH 块必须重复恰好 10 次。如果重复9次就OK了。
我猜想由于 BOOST_FOREACH 使用模板欺骗(const auto_any),编译器假设迭代器句柄是常量,并且永远不会再次读取其实际值。
我很高兴听到我的代码是错误的,纠正它并继续使用 BOOST_FOREACH,我很发现它(而不是 BOOST_FOREVER :)。
可能与:为什么 BOOST_FOREACH 有时不适用于 C++ 字符串? https://stackoverflow.com/questions/1275852/why-does-boost-foreach-not-work-sometimes-with-c-strings
EDIT:
我准备了重现问题的简化项目。没有模板,没有默认参数,什么都没有。在这里获取:http://yabcok.nazwa.pl/ugly3.zip http://yabcok.nazwa.pl/ugly3.zip