473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6631
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
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...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.