473,396 Members | 1,724 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,396 software developers and data experts.

How to convert from std::list<T*>::iterator to std::list<const T*>::iterator?

Hi,

Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.

However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?

Best wishes,
Peng

Oct 27 '05 #1
6 6625
Pe*******@gmail.com wrote:
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
Store const pointers. Or convert upon extraction.

Object const* p = *iter;
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.
Huh?
However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?


There is no way. You can copy std::list<T*> into std::list<T const*>,
but there is no conversion between the containers or between their
respective iterators. T* and T const* are different types and the
templates instantiated for them are not related.

V
Oct 27 '05 #2

Pe*******@gmail.com wrote:
Hi,

Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.

However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?


How about

reinterpret_cast<std::list<const T *> &>(original_list)

to interpret the list as a list of const T * pointers? It's a hack, but
innocous enough. The templates should be binary compatible, so you are
down to some obscure undefined behavior related to aliasing.

Or else you could implement an iterator proxy object which wraps around
an existing iterator but adds const qualification to any extracted
pointers.

converting_iter<const T *, std::list<T *>::iterator>
cui = original_list.begin();

The converting_iter template has two parameters: the iterator type to
be wrapped, and the type that the extracted values should be cast to.

Of course converting_iter has a converting constructor that takes a
reference to the iterator type being wrapped, so it can swallow the
object being proxied. Supporting assigment from that type might be
handy too.

It has to proxy all of the iter operations and pass them to the
captured object: things like ++, and so on. The ones that pull out a T
* are intercepted and wrapped with a static_cast<const T *>.

This could be used in other ways, e.g.

converting_iter<float, std::vector<int>::iterator>

would give you an converting iterator type that wraps iterators for a
std::vector<int>, with an interator that pulls out the values converted
to float.

Oct 27 '05 #3
On 2005-10-27, Pe*******@gmail.com <Pe*******@gmail.com> wrote:
Hi,

Suppose I have a list which contains pointers. I want the
pointer got by dereferencing the iterator be a pointer pointing
to a const object. But std::list<const T*>::const_iterator
doens't give me this capability. So I want
std::list<T*>::iterator.

However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?


Try wrapping your pointers in a very simple wrapper class.

Boost:: might already provide such a thing. In which case, you
should use it instead of the pretty much untested example code
below.

/* Allow only const access to the item pointed to. */

template <typename T>
class const_ptr {
T* ptr;
public:
const_ptr(T* p = 0): ptr(p) { }
const T& operator*() { return *ptr; }
const T* operator->() { return ptr; }
};

int main()
{
/* For example: */
std::list< const_ptr<int> > cpl;
return 0;
}

--
Neil Cerutti
Oct 27 '05 #4

Neil Cerutti wrote:
On 2005-10-27, Pe*******@gmail.com <Pe*******@gmail.com> wrote:
Hi,

Suppose I have a list which contains pointers. I want the
pointer got by dereferencing the iterator be a pointer pointing
to a const object. But std::list<const T*>::const_iterator
doens't give me this capability. So I want
std::list<T*>::iterator.

However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?


Try wrapping your pointers in a very simple wrapper class.

Boost:: might already provide such a thing. In which case, you
should use it instead of the pretty much untested example code
below.

/* Allow only const access to the item pointed to. */

template <typename T>
class const_ptr {
T* ptr;
public:
const_ptr(T* p = 0): ptr(p) { }
const T& operator*() { return *ptr; }
const T* operator->() { return ptr; }
};

int main()
{
/* For example: */
std::list< const_ptr<int> > cpl;
return 0;
}


Do you know which boost package should I use?

Thanks,
Peng

Oct 27 '05 #5
On 2005-10-27, Pe*******@gmail.com <Pe*******@gmail.com> wrote:

Neil Cerutti wrote:
On 2005-10-27, Pe*******@gmail.com <Pe*******@gmail.com> wrote:
> Hi,
>
> Suppose I have a list which contains pointers. I want the
> pointer got by dereferencing the iterator be a pointer pointing
> to a const object. But std::list<const T*>::const_iterator
> doens't give me this capability. So I want
> std::list<T*>::iterator.
>
> However, the container is of type std::list<T*>. How to get
> std::list<const T*>::iterator?


Try wrapping your pointers in a very simple wrapper class.

Boost:: might already provide such a thing. In which case, you
should use it instead of the pretty much untested example code
below.

/* Allow only const access to the item pointed to. */

template <typename T>
class const_ptr {
T* ptr;
public:
const_ptr(T* p = 0): ptr(p) { }
const T& operator*() { return *ptr; }
const T* operator->() { return ptr; }
};

int main()
{
/* For example: */
std::list< const_ptr<int> > cpl;
return 0;
}


Do you know which boost package should I use?


Well, it turns out my suggestion was silly, and amounted to the
same thing as using a list<const T*>.

From Boost, what you might want is something from the group
of iterator adaptors. Specifically, the transform iterator
adaptor.

--
Neil Cerutti
Oct 28 '05 #6
<Pe*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't give me this
capability. So I want std::list<T*>::iterator.

However, the container is of type std::list<T*>. How to get
std::list<const T*>::iterator?

Best wishes,
Peng


would std::list<T*>::const_iterator work for you?
Oct 30 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ishekar | last post by:
Hi all, Dont hit me for asking this in this forum but i feel this question is related to allocators hence i think its the right place. I was trying to use the list of CComBSTR but was getting...
5
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++...
9
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
12
by: arnuld | last post by:
It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * ...
9
by: t | last post by:
Can you use a plain iterator to vector<const intto write to it? It doesn't seem like it should be possible, but Visual C++ 2005 Express lets me: vector<const intvc; vc.push_back(5);...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
3
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.