Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iterator and const_iterator

Reply
Thread Tools

iterator and const_iterator

 
 
John Harrison
Guest
Posts: n/a
 
      11-20-2005
I there a reliable and generic method to convert a const_iterator to an
iterator (i.e. something like const_cast).

I ask because I'm writing some methods which take and return iterators.
A const version of the method takes and returns const iterators and the
non-const version takes and returns non-const iterators. I'd like to
avoid code duplication like this

class X
{
iterator some_method(iterator i)
{
...
}

const_iterator some_method(const_iterator i) const
{
return const_cast<X*>(this)->some_method(i);
}

}

But that only works if I can convert a const_iterator to the equivalent
iterator.

Answers or alternatives welcome.

john
 
Reply With Quote
 
 
 
 
peter steiner
Guest
Posts: n/a
 
      11-20-2005
> class X
> {
> iterator some_method(iterator i)
> {
> ...
> }
>
> const_iterator some_method(const_iterator i) const
> {
> return const_cast<X*>(this)->some_method(i);
> }
>
> }


even if this cast would be possible the const-qualified some_method
would always violate the constness if you modify the instance later on
inside the first non-const-qualified some_method.

are you sure that this does not happen? i don't see how the first
some_method overload could guarantee that after casting the constness
of this and the iterator argument away.

maybe i misunderstand your basic idea but as i see it in the example
your method could be templated for the iterator argument and should
then either be const-qualified or not, depending if you modify the
instance itself or not.

-- peter

 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      11-20-2005
John Harrison wrote in news:ay3gf.3573$(E-Mail Removed) in
comp.lang.c++:

> I there a reliable and generic method to convert a const_iterator to an
> iterator (i.e. something like const_cast).
>


>
> Answers or alternatives welcome.
>


Defenitvly alternative, but ...

template < typename iterator >
iterator some_method( X const &that, iterator i );

class X
{
iterator some_method(iterator i)
{
return some_method( *this, i );
}

const_iterator some_method(const_iterator i) const
{
return some_method( *this, i );
}
}

template < typename iterator >
iterator some_method( X const &that, iterator i )
{
// real code here
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-20-2005
peter steiner wrote:
>>class X
>>{
>> iterator some_method(iterator i)
>> {
>> ...
>> }
>>
>> const_iterator some_method(const_iterator i) const
>> {
>> return const_cast<X*>(this)->some_method(i);
>> }
>>
>>}

>
>
> even if this cast would be possible the const-qualified some_method
> would always violate the constness if you modify the instance later on
> inside the first non-const-qualified some_method.


I should have made that clear. some_method don't not itself modify
anything, but it may (in the non-const version) return an iterator to
the internals of X which could be used to modify an X object.

Many methods in the STL have this property, for instance the lower_bound
and upper_bound methods of std::map.

>
> are you sure that this does not happen? i don't see how the first
> some_method overload could guarantee that after casting the constness
> of this and the iterator argument away.
>
> maybe i misunderstand your basic idea but as i see it in the example
> your method could be templated for the iterator argument and should
> then either be const-qualified or not, depending if you modify the
> instance itself or not.


Yes templates occurred to me after I'd posted, but I wonder if there is
any method that avoids essentially have two copies of the same code.

>
> -- peter
>


john
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-20-2005
Rob Williscroft wrote:
> John Harrison wrote in news:ay3gf.3573$(E-Mail Removed) in
> comp.lang.c++:
>
>
>>I there a reliable and generic method to convert a const_iterator to an
>>iterator (i.e. something like const_cast).
>>

>
>
>>Answers or alternatives welcome.
>>

>
>
> Defenitvly alternative, but ...
>
> template < typename iterator >
> iterator some_method( X const &that, iterator i );
>
> class X
> {
> iterator some_method(iterator i)
> {
> return some_method( *this, i );
> }
>
> const_iterator some_method(const_iterator i) const
> {
> return some_method( *this, i );
> }
> }
>
> template < typename iterator >
> iterator some_method( X const &that, iterator i )
> {
> // real code here
> }
>
> Rob.


Templates do the trick as far as avoiding writing the same code twice. I
guess that's the best that can be done.

john
 
Reply With Quote
 
Cy Edmunds
Guest
Posts: n/a
 
      11-20-2005
"John Harrison" <(E-Mail Removed)> wrote in message
news:ay3gf.3573$(E-Mail Removed)...
>I there a reliable and generic method to convert a const_iterator to an
>iterator (i.e. something like const_cast).
>
> I ask because I'm writing some methods which take and return iterators. A
> const version of the method takes and returns const iterators and the
> non-const version takes and returns non-const iterators. I'd like to avoid
> code duplication like this
>
> class X
> {
> iterator some_method(iterator i)
> {
> ...
> }
>
> const_iterator some_method(const_iterator i) const
> {
> return const_cast<X*>(this)->some_method(i);
> }
>
> }
>
> But that only works if I can convert a const_iterator to the equivalent
> iterator.
>
> Answers or alternatives welcome.
>
> john


If iterator is derived from const_iterator, which in many cases is perfectly
practical, then you can do this:

class X
{
private:
iterator some_method_impl(const_iterator i) {...} // cast as required
here
public:
iterator some_method(iterator i) {return some_method_impl(i);} // bit
slice argument
const_iterator some_method(const_iterator i) const {return
some_method_impl(i);} // bit slice result
};

Of course I don't know if this would work with your particular iterators.
With many iterators in my experience, the bit sliced version is actually the
same data.

--
Cy
http://home.rochester.rr.com/cyhome/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
iterator vs const_iterator Victor Bazarov C++ 3 07-21-2006 03:50 PM
implementing const_iterator and iterator flopbucket C++ 1 06-21-2006 08:49 PM
comparing iterator with const_iterator Mark P C++ 2 06-15-2006 06:21 PM
How the comparison operators are defined for iterator and const_iterator PengYu.UT@gmail.com C++ 2 10-23-2005 04:33 PM
Questions about Getting const_iterator out of iterator class CoolPint C++ 3 12-13-2003 04:13 AM



Advertisments