iterator_adapter conversion to base iterator?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

iterator_adapter conversion to base iterator?

Sebastian Weber
Hello everybody!

Please don't blame for beeing foolish - I'm using the iterator-adapter
class for the first time. I converted a standard iterator from a
std::vector with the iterator_adapter stuff into another class
random_set_iterator. But at certain points in my programm I need the
random_set_iterator converted back to the original iterator class from
the vector class. I noticed the base()-function which basically does the
job, but I think there should be a more elegant and clearer way to do
it. Am I wrong or shouldn't an adapted iterator almost always be
convertible to it's base iterator? I'm sure, it should not hard to
implement somthing like this, but how?

Thanks in advance!

Greetings,

Sebastian

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: iterator_adapter conversion to base iterator?

David Abrahams
Sebastian Weber <[hidden email]> writes:

> Hello everybody!
>
> Please don't blame for beeing foolish - I'm using the iterator-adapter
> class for the first time. I converted a standard iterator from a
> std::vector with the iterator_adapter stuff into another class
> random_set_iterator. But at certain points in my programm I need the
> random_set_iterator converted back to the original iterator class from
> the vector class. I noticed the base()-function which basically does the
> job, but I think there should be a more elegant and clearer way to do
> it. Am I wrong or shouldn't an adapted iterator almost always be
> convertible to it's base iterator?

It shouldn't.  Implicit conversions are generally dangerous; there's
no good reason to allow one here.

> I'm sure, it should not hard to
> implement somthing like this, but how?

You can easily add an

   operator base_iterator_type() const

to random_set_iterator, but we (the authors of iterator_adaptor) don't
recommend it.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: iterator_adapter conversion to base iterator?

Sebastian Weber
Hi!

> It shouldn't.  Implicit conversions are generally dangerous; there's
> no good reason to allow one here.

Well, that might be in general the case. But at least in my case I think
it does make sense: random_set_iterator is basically a
std::vector<>::iterator with the noteable difference that the
next-operation ++ is replaced by a function which moves the iterator to
a random element in within the vector. And as I do want to delete
elements from the vector which I find by the random-iterator, I need the
conversion.

>
> > I'm sure, it should not hard to
> > implement somthing like this, but how?
>
> You can easily add an
>
>    operator base_iterator_type() const
>
> to random_set_iterator, but we (the authors of iterator_adaptor) don't
> recommend it.

Thanks, that did it.


Greetings,

Sebastian Weber

>

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: iterator_adapter conversion to base iterator?

David Abrahams
Sebastian Weber <[hidden email]> writes:

> Hi!
>
>> It shouldn't.  Implicit conversions are generally dangerous; there's
>> no good reason to allow one here.
>
> Well, that might be in general the case. But at least in my case I think
> it does make sense: random_set_iterator is basically a
> std::vector<>::iterator with the noteable difference that the
> next-operation ++ is replaced by a function which moves the iterator to
> a random element in within the vector. And as I do want to delete
> elements from the vector which I find by the random-iterator, I need the
> conversion.

Surely you don't *need* it.  v.erase(x.base()) will work just fine.

>> > I'm sure, it should not hard to
>> > implement somthing like this, but how?
>>
>> You can easily add an
>>
>>    operator base_iterator_type() const
>>
>> to random_set_iterator, but we (the authors of iterator_adaptor) don't
>> recommend it.
>
> Thanks, that did it.

You're welcome.  But just remember, we warned you.  Don't be surprised
if you find that you are seeing unexpected effects due to the implicit
conversion.  For example, your iterators random_set_iterator is
probably not random access, but I bet you can now write i1 < i2.
Stranger effects where code compiles but does something you didn't
intend are possible.

Good Luck,

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users