Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing collection elements with const iterators

1 view
Skip to first unread message

Adam Badura

unread,
Sep 14, 2010, 10:49:13 AM9/14/10
to
What is the point of const iterators being unusable for elements
removal?

I mean that if I have a const collection object then no matter what
iterators I have I will not be able to erase any element. But if I
have a non-const collection object then why am I not allowed to erase
its elements with a const iterator? After all I could as well take new
non-const begin and advance it until it is equal to the const iterator
and use such iterator for erasure.

Adam Badura

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Bo Persson

unread,
Sep 15, 2010, 8:07:15 AM9/15/10
to
Adam Badura wrote:
> What is the point of const iterators being unusable for elements
> removal?
>
> I mean that if I have a const collection object then no matter what
> iterators I have I will not be able to erase any element. But if I
> have a non-const collection object then why am I not allowed to
> erase its elements with a const iterator? After all I could as well
> take new non-const begin and advance it until it is equal to the
> const iterator and use such iterator for erasure.
>
> Adam Badura

You are right, this will be fixed in the next standard, C++0x.

Bo Persson

SG

unread,
Sep 15, 2010, 8:05:50 AM9/15/10
to
On 14 Sep., 16:49, Adam Badura wrote:
>
> =A0 What is the point of const iterators being unusable for elements
> removal?
>
> =A0 I mean that if I have a const collection object then no matter what

> iterators I have I will not be able to erase any element. But if I
> have a non-const collection object then why am I not allowed to erase
> its elements with a const iterator?

The simple answer is: Because the C++ standard says so. And it says so
because nobody pushed for a different erase member functions.

BTW: The upcoming C++ standard actually fixes this.

Cheers!
SG

Daniel Krügler

unread,
Sep 15, 2010, 8:06:36 AM9/15/10
to
On 14 Sep., 16:49, Adam Badura <abad...@o2.pl> wrote:
> =A0 =A0 =A0 =A0 What is the point of const iterators being unusable for e=
lements
> removal?
>
> =A0 =A0 =A0 =A0 I mean that if I have a const collection object then no m=

atter what
> iterators I have I will not be able to erase any element. But if I
> have a non-const collection object then why am I not allowed to erase
> its elements with a const iterator? After all I could as well take new
> non-const begin and advance it until it is equal to the const iterator
> and use such iterator for erasure.

Perfectly right - therefore the Standard Library has fixed this
awful state by accepting LWG issue:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#180

as a defect and applying the proposed resolution published in

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2350.pdf

The current working draft has these changes implemented.

HTH & Greetings from Bremen,

Daniel Kr=FCgler

Ulrich Eckhardt

unread,
Sep 16, 2010, 12:52:57 PM9/16/10
to
Adam Badura wrote:
> [...] if I have a const collection object then no matter what

> iterators I have I will not be able to erase any element. But
> if I have a non-const collection object then why am I not allowed
> to erase its elements with a const iterator? After all I could as
> well take new non-const begin and advance it until it is equal to
> the const iterator and use such iterator for erasure.

This is a known problem. In other words, an iterator has three different
interfaces:
1. Marking a position in a sequence.
2. Read access to the underlying element.
3. Write access to the underlying element.

For erasing, only the first interface is actually necessary, but there is=
no
distinct type common to both "iterator" and "const_iterator" that provide=
s
this interface, instead the containers all want the "iterator" as
parameter.

Uli

--
Sator Laser GmbH
Gesch=C3=A4ftsf=C3=BChrer: Thorsten F=C3=B6cking, Amtsgericht Hamburg HR =
B62 932

0 new messages