C ++标准:multiset中意外的const_iterator [英] C++ Standard: Unexpected const_iterator in multiset

查看:261
本文介绍了C ++标准:multiset中意外的const_iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个奇怪的问题,我在迭代时得到了 const_iterator 而不是预期的迭代器通过multiset。原来是MSVC的一个非问题,但g ++给了我一个错误:

lockquote

错误:
引用无效初始化类型'myPtr&'从
类型'const
boost :: shared_ptr'的表达式'

相关代码:

  typedef std :: multiset< myPtr>我的列表; 
myList _mystuff;
void tick(float dt)
{
for(myList :: iterator i = _mystuff.begin(); i!= _mystuff.end(); ++ i)
{
myPtr& mine = * i; //这里是g ++的问题,不是MSVC
// const myPtr& mine = * i;适用于g ++
mine-> tick(dt);
}
}

相当多的研究表明,很多以前的讨论。我发现了这些相关的部分:



我对这个问题的背景知识和把握有限,因此我想知道是否标准没有很好地定义这种行为,在这种情况下,g ++和MSVC根据自己的喜好实现行为,或者g ++或MSVC偏离定义良好的标准。


解决方案

set和multiset的迭代器已从标准迭代器/常量迭代器对改为正在常量迭代器。这个改变的原因是它们是有序的容器,而改变迭代器内部的元素实际上可以使这个排序约束失效。



您要测试的GCC版本反对做了这个改变,你使用的VC版本还没有。 VC10(和VC9 SP1,我相信)总是从集合和多集合中返回const_iterators。



最新C ++ 1x草案的23.2.4 / 6(n3000.pdf )b
$ b


对于其中
的值类型与键
相同的关联容器, iterator和const_iterator
都是常量迭代器。

std :: set和std :: multi_set是关联容器,其中值类型与键类型相同。


I recently ran into an odd issue where I'd get a const_iterator instead of the expected iterator when iterating through a multiset. It turned out to be a non-issue for MSVC but g++ gave me an error:

error: invalid initialization of reference of type 'myPtr&' from expression of type 'const boost::shared_ptr'

Relevant code:

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

Quite a bit of research revealed that is a problem with lots of previous discussion. I found these relevant bits:

My background knowledge and grasp on the issue is limited and thus I'd like to know whether the standard doesn't define this behavior well enough in which case g++ and MSVC implement the behavior to their liking or whether either g++ or MSVC deviate from a well-defined standard.

Thanks in advance.

解决方案

The iterators for set and multiset were changed from the standard iterator/const iterator pair to just being const iterators. The reason for this change was that they are ordered containers, and changing the element inside of an iterator can actually invalidate this ordering constraint.

The version of GCC you're testing against has made this change, the version of VC that you're using has not. VC10 (and VC9 SP1, I believe) always return const_iterators from sets and multisets.

23.2.4/6 of the latest draft of C++1x (n3000.pdf at the moment) says

For associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators.

std::set and std::multi_set are the associative containers where the value type is the same as the key type.

这篇关于C ++标准:multiset中意外的const_iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆