免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: nmglzlong
打印 上一主题 下一主题

[C++] 一段代码VC下编译通过,GCC则不行为什么 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-07-24 17:13 |只看该作者
这是C++标准的规定。对于模板类,编译期无法判断list<T>::iterator是类型还是变量(常量),因此需要typename关键字来标识类型。

论坛徽章:
0
12 [报告]
发表于 2008-07-24 17:15 |只看该作者
179. Comparison of const_iterators to iterators doesn't work
Section: 23.1 [lib.container.requirements]  Status: WP  Submitter: Judy Ward  Date: 2 Jul 1998

Currently the following will not compile on two well-known standard library implementations:

#include <set>
using namespace std;

void f(const set<int> &s)
{
  set<int>::iterator i;
  if (i==s.end()); // s.end() returns a const_iterator
}The reason this doesn't compile is because operator== was implemented as a member function of the nested classes set:iterator and set::const_iterator, and there is no conversion from const_iterator to iterator. Surprisingly, (s.end() == i) does work, though, because of the conversion from iterator to const_iterator.

I don't see a requirement anywhere in the standard that this must work. Should there be one? If so, I think the requirement would need to be added to the tables in section 24.1.1. I'm not sure about the wording. If this requirement existed in the standard, I would think that implementors would have to make the comparison operators non-member functions.

This issues was also raised on comp.std.c++ by Darin Adler.  The example given was:

bool check_equal(std::deque<int>::iterator i,
std::deque<int>::const_iterator ci)
{
return i == ci;
}Comment from John Potter:

In case nobody has noticed, accepting it will break reverse_iterator.

The fix is to make the comparison operators templated on two types.

    template <class Iterator1, class Iterator2>
    bool operator== (reverse_iterator<Iterator1> const& x,
                     reverse_iterator<Iterator2> const& y);
    Obviously: return x.base() == y.base();

Currently, no reverse_iterator to const_reverse_iterator compares are valid.

BTW, I think the issue is in support of bad code. Compares should be between two iterators of the same type. All std::algorithms require the begin and end iterators to be of the same type.

Proposed resolution:

Insert this paragraph after 23.1 [lib.container.requirements] paragraph 7:

In the expressions

    i == j
    i != j
    i < j
    i <= j
    i >= j
    i > j
    i - j
  Where i and j denote objects of a container's iterator type, either or both may be replaced by an object of the container's const_iterator type referring to the same element with no change in semantics.

[post-Toronto: Judy supplied a proposed resolution saying that iterator and const_iterator could be freely mixed in iterator comparison and difference operations.]

[Redmond: Dave and Howard supplied a new proposed resolution which explicitly listed expressions; there was concern that the previous proposed resolution was too informal.]

Rationale:

The LWG believes it is clear that the above wording applies only to the nested types X::iterator and X::const_iterator, where X is a container. There is no requirement that X::reverse_iterator and X::const_reverse_iterator can be mixed. If mixing them is considered important, that's a separate issue.

论坛徽章:
0
13 [报告]
发表于 2008-07-24 17:19 |只看该作者
个人感觉是g++用的库或许有问题
只有
iterator end()
而没有
const_iterator end()

猜测而已 你可以自己看看你的list怎么实现的

论坛徽章:
0
14 [报告]
发表于 2008-07-24 17:49 |只看该作者
好的谢谢各位了。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP