473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

iterator and const_iterator

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
5 4780
> 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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
8
by: Alexander Stippler | last post by:
Hello, the standard requires a type reverse_iterator. But in the form it is required and implemented for all compilers I know, there is IMO something missing, which is essential for its usage....
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
6
by: Rares Vernica | last post by:
Hi, I am using tr1/unordered_map in my code. My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5). I get...
3
by: gallows | last post by:
The container is: template <typename T> class Container { public: // container methods.. // iterator: class const_iterator { public:
3
by: jarek | last post by:
Hi! How can I declare iterator to const object ? I've the following: class ClassA : public list<ClassB> { ..... }
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.