我最近遇到了一个奇怪的问题,我在迭代multiset时得到了const_iterator
而不是预期的iterator
。它原来是一个非问题的MSVC但G ++给了我一个错误:C++标准:multiset中意外的const_iterator
error: invalid initialization of reference of type 'myPtr&' from expression of type 'const boost::shared_ptr'
相关代码:
typedef std::multiset<myPtr> myList;
myList _mystuff;
void tick(float dt)
{
for (myList::iterator i = _mystuff.begin(); i != _mystuff.end(); ++i)
{
myPtr &mine = *i; // g++ problem here, not for MSVC
// const myPtr &mine = *i; works fine for g++
mine->tick(dt);
}
}
相当多的研究发现,与很多以前讨论的问题。我发现这些相关位:
- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14990
- http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#322
- http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#103
- http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#279
- http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#528
我的背景知识,在这个问题上把握是有限的,因此,我想想知道标准是否没有定义这一点行为足够好,在这种情况下,g ++和MSVC根据自己的喜好实施行为,或者g ++或MSVC是否偏离定义良好的标准。
在此先感谢。
改变 '我的' 的DECL到'常量myPtr&mine'。当然,'tick'必须声明为'void tick(float)const;'并且由tick修改的任何数据成员都需要是'mutable'。 – KitsuneYMG 2010-01-10 21:18:04