Topic: VC.NET and iterator subtraction


Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Sat, 17 Nov 2001 00:26:20 GMT
Raw View
In article <9srr32$lhd$1@acs2.byu.edu>, Adam Peterson says...
>
>> Operator- then can't determine the distance between C::iterator and
>> C::const_iterator because these types are basically unrelated, even
>> though C::const_iterator can be converted to C::iterator
>
>I don't think C::const_iterator can be converted to C::iterator without an
>explicit const_cast.

const_cast<>'s don't convert between different types. C::iterator and
C::const_iterator are two unrelated types, according to the core language.
There is no conversion from const_iterator to non-const iterator.

>I believe C::iterator can be converted to C::const_iterator, though.

Yeah, I got it one backwards. The library design is such you can take away
rights, not add them.

Regards,

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Adam Peterson <ahp6@email.byu.edu>
Date: Mon, 19 Nov 2001 16:55:28 GMT
Raw View
> >I don't think C::const_iterator can be converted to C::iterator without
an
> >explicit const_cast.
>
> const_cast<>'s don't convert between different types. C::iterator and
> C::const_iterator are two unrelated types, according to the core language.
> There is no conversion from const_iterator to non-const iterator.

Yes, you're right, of course.  However, IIRC sometimes iterators are
implemented as pointers, and these pointers are cv-related, so these can be
converted using const-casts.  But it is not guaranteed and should not be
relied on.  In any event, as you say, it _can't_ be done without a
const_cast<> (or portably even with one).



---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Adam Peterson <ahp6@email.byu.edu>
Date: Wed, 14 Nov 2001 10:28:25 GMT
Raw View
> Operator- then can't determine the distance between C::iterator and
> C::const_iterator because these types are basically unrelated, even
> though C::const_iterator can be converted to C::iterator

I don't think C::const_iterator can be converted to C::iterator without an
explicit const_cast.
I believe C::iterator can be converted to C::const_iterator, though.


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Scott Andrew" <sandrew@dsli.com>
Date: Sat, 10 Nov 2001 21:33:49 GMT
Raw View
I am using Visual Studio .NET (Dev Studio 7). I have the following code.
This looks weird but it's only to show the problem it's not the exact way
this is being used.

typedef vector<foostruct> FooVector;
teypdef FooVector::iterator FooIterator;

FooVector myData;

.... do some work to fill the vector with 100 items.

FooIteraor someData = myData.begin() + 20;

... do some stuff.

someData = myData.begin() + (someData - myData.begin()); // this is the
compiler error

the error is that there is no operator - that takes a right hand side
const_iterator. I can cast away the const or typecast someData as const (it
really isn't becuase it's data get's changed). But is there something in the
standard that says by default vector::begin() returns const_iterators and
you can do the subtraction above?

Scott Andrew





---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Mon, 12 Nov 2001 11:19:32 GMT
Raw View
In article <d5fH7.24010$lV4.2438404@e420r-atl1.usenetserver.com>, Scott Andrew
says...
>
>I am using Visual Studio .NET (Dev Studio 7). I have the following code.
>This looks weird but it's only to show the problem it's not the exact way
>this is being used.
>
>typedef vector<foostruct> FooVector;
>teypdef FooVector::iterator FooIterator;
>
>FooVector myData;
>
>.... do some work to fill the vector with 100 items.
>
>FooIteraor someData = myData.begin() + 20;

And of course someData can be used to change the data.
>
>... do some stuff.
>
>someData = myData.begin() + (someData - myData.begin()); // this is the
>compiler error
>
>the error is that there is no operator - that takes a right hand side
>const_iterator. I can cast away the const or typecast someData as const (it
>really isn't becuase it's data get's changed). But is there something in the
>standard that says by default vector::begin() returns const_iterators and
>you can do the subtraction above?
>
>Scott Andrew

The standard says that vector::begin() returns an iterator, and
vector::begin() const returns a const_iterator. You should have gotten
an iterator, according to Table 65 in the standard, because myData is
non-const. This is basic overloading, where the object type is the
only parameter. According to Table 9, an identical match is better than
a qualification adjustment (adjusting myData to const vector type).

That should happen, but doesn't. It looks like a problem in VC7(core)
in overload resolution on members.

Operator- then can't determine the distance between C::iterator and
C::const_iterator because these types are basically unrelated, even
though C::const_iterator can be converted to C::iterator

FWIW, I therefore think const_iterator is a bad concept. Its type
interferes too much with generic programming. I just don't have anything
better.

Regards,

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl

---
[ 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.research.att.com/~austern/csc/faq.html                ]