Topic: Deprecate const_iterator and use const iterator instead


Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Tue, 28 Feb 2006 15:34:14 GMT
Raw View
 Is there any chance to deprecate const_iterator and propose to use const
iterator instead? I.e. explicitly stand that const_iterator is always a const
iterator typedef?
 IMHO the const_iterator concept was got wrong in the original STL design and
there exist a lot of DRs on this "deficient iterator and const_iterator
coexistence" topic.
 In particular, vector is allowed to define vector::const_iterator as const
vector::iterator (no other way to allow plain T* as iterator, though) so why
can't we extend this clever rule a little bit further?
--
         With all respect, Sergey.               http://ders.angen.net/
         mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Andrew Koenig" <ark@acm.org>
Date: Tue, 28 Feb 2006 10:57:28 CST
Raw View
""Sergey P. Derevyago"" <non-existent@iobox.com> wrote in message
news:44046BF5.7B3237C6@iobox.com...

> Is there any chance to deprecate const_iterator and propose to use const
> iterator instead? I.e. explicitly stand that const_iterator is always a
> const
> iterator typedef?

Nope.

If x is an array with elements of type T, then a variable of type T* can
serve as an iterator over the elements of x.

The corresponding const_iterator type would be const T*, or, equivalently, T
const *, both of which mean "pointer to const T"

Your proposed "const iterator" type would be T* const, which can still be
used to change elements of x, and is therefore not appropriate to serve as a
const iterator.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Tue, 28 Feb 2006 18:10:11 GMT
Raw View
Andrew Koenig wrote:
> Your proposed "const iterator" type would be T* const, which can still be
> used to change elements of x, and is therefore not appropriate to serve as a
> const iterator.
>
 I missed the point, sorry.

 OK, what about the following design:
1. Every container shall define basic_iterator<> member template.
2. Requred iterator and const_iterator member typedefs are always
basic_iterator<T>::iterator and basic_iterator<const T>::iterator
respectively.

 In particular, vector may use the following definition:

template <class T> struct basic_iterator { typedef T* iterator; };
--
         With all respect, Sergey.               http://ders.angen.net/
         mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Bob Bell" <belvis@pacbell.net>
Date: Tue, 28 Feb 2006 18:50:00 CST
Raw View
"Sergey P. Derevyago" wrote:
>  OK, what about the following design:
> 1. Every container shall define basic_iterator<> member template.
> 2. Requred iterator and const_iterator member typedefs are always
> basic_iterator<T>::iterator and basic_iterator<const T>::iterator
> respectively.
>
>  In particular, vector may use the following definition:
>
> template <class T> struct basic_iterator { typedef T* iterator; };

What about it? It sounds like you're describing a particular
implementation of container, iterator and const_iterator. How does that
impact the standard? What am I missing?

Bob

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: kuyper@wizard.net
Date: Tue, 28 Feb 2006 22:12:28 CST
Raw View
"Sergey P. Derevyago" wrote:
> Andrew Koenig wrote:
> > Your proposed "const iterator" type would be T* const, which can still be
> > used to change elements of x, and is therefore not appropriate to serve as a
> > const iterator.
> >
>  I missed the point, sorry.
>
>  OK, what about the following design:
> 1. Every container shall define basic_iterator<> member template.
> 2. Requred iterator and const_iterator member typedefs are always
> basic_iterator<T>::iterator and basic_iterator<const T>::iterator
> respectively.
>
>  In particular, vector may use the following definition:
>
> template <class T> struct basic_iterator { typedef T* iterator; };

I think you need to explain more clearly what the problem is that
you're trying to solve, and how this proposed change helps solve it.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Richard Smith" <richard@ex-parrot.com>
Date: Tue, 28 Feb 2006 22:12:02 CST
Raw View
"Sergey P. Derevyago" wrote:
> Is there any chance to deprecate const_iterator and propose to use const
> iterator instead? I.e. explicitly stand that const_iterator is always a const
> iterator typedef?

But a const_iterator cannot be a typedef to const iterator -- they're
completely different things!  For example, you can increment a
const_iterator, but not a const iterator; conversely, you can assign to
the underlying value of a const iterator, but not to that of a
const_iterator.

>  In particular, vector is allowed to define vector::const_iterator as const
> vector::iterator.

No, it isn't, and I'm certain that no vendors do -- it would be
unusable.

> (no other way to allow plain T* as iterator, though)

Note that if iterator is defined as a typedef to 'T*', then 'const
iterator' is a synonym for 'T* const', *not* for 'const T*'.

--
Richard Smith

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: kuyper@wizard.net
Date: Wed, 1 Mar 2006 00:52:45 CST
Raw View
Bob Bell wrote:
> "Sergey P. Derevyago" wrote:
> >  OK, what about the following design:
> > 1. Every container shall define basic_iterator<> member template.
> > 2. Requred iterator and const_iterator member typedefs are always
> > basic_iterator<T>::iterator and basic_iterator<const T>::iterator
> > respectively.
> >
> >  In particular, vector may use the following definition:
> >
> > template <class T> struct basic_iterator { typedef T* iterator; };
>
> What about it? It sounds like you're describing a particular
> implementation of container, iterator and const_iterator. How does that
> impact the standard? What am I missing?

The impact on the standard is that he's proposing that this particular
type of implementation be mandatory, not merely permitted. I'm not sure
why - but that's what he seems to be proposing.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Markus Moll <moll@rbg.informatik.tu-darmstadt.de>
Date: Wed, 1 Mar 2006 09:38:38 CST
Raw View
Hi

Sergey P. Derevyago wrote:

> I missed the point, sorry.
>
> OK, what about the following design:
> 1. Every container shall define basic_iterator<> member template.
> 2. Requred iterator and const_iterator member typedefs are always
> basic_iterator<T>::iterator and basic_iterator<const T>::iterator
> respectively.

You're aware of the fact that this would just be a clumsy notation for
iterator and const_iterator, as you would (specialization!) still be free
to have two totally distinct and unrelated classes
basic_iterator<T>::iterator and basic_iterator<const T>::iterator?

Markus

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Wed, 1 Mar 2006 15:38:32 GMT
Raw View
kuyper@wizard.net wrote:
> I think you need to explain more clearly what the problem is that
> you're trying to solve, and how this proposed change helps solve it.
>
 OK, I'm going to bring something useful in a few days.
 Sorry for those confusing messages.
--
         With all respect, Sergey.               http://ders.angen.net/
         mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Joe" <jgreer@nsisoftware.com>
Date: Wed, 1 Mar 2006 10:41:59 CST
Raw View
Bob Bell wrote:

> What about it? It sounds like you're describing a particular
> implementation of container, iterator and const_iterator. How does that
> impact the standard? What am I missing?
>
> Bob
>

It sounds to me like he is after a different spelling of
const_iterator.  A some level, I can see the desire and have fallen
into the trap in my youth of having a typedef like:

typedef My * MyPtr;

then wanting to declare

const MyPtr pMy;

and have pMy really be a const My * rather than a My * const.  However,
I got over it.  I think the major confusion comes from typedefs being
aliases and neither a real type nor a macro-like substitution.  This
seems to confuse people (especially novices).

So, with iterators, I can see that at first blush you might want const
iterator to be the same as const_iterator, however, this is the point
at which you get to learn the difference between a typedef and a
macro-like thing and not a place where the language needs to change.
Any change here would cause more confusion than it would remove, IMO.

joe

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]