By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,307 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,307 IT Pros & Developers. It's quick & easy.

iterator and const_iterator

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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

Nov 22 '05 #2

P: n/a
John Harrison wrote in news:ay****************@newsfe3-win.ntli.net 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/
Nov 22 '05 #3

P: n/a
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
Nov 22 '05 #4

P: n/a
Rob Williscroft wrote:
John Harrison wrote in news:ay****************@newsfe3-win.ntli.net 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
Nov 22 '05 #5

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
news:ay****************@newsfe3-win.ntli.net...
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/
Nov 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.